Create a Docker network and launch NATS
A shared network lets the containers talk by name. JetStream is enabled with -js.
docker network create nuts-demo
docker run -d --name nats --network nuts-demo \
-p 4222:4222 nats:2.12-alpine -js
bartosz@idct:~/software/nuts$ cat QUICKSTART.md
Spin up NATS, create a stream, run NUTS, open a page and watch messages arrive in real time. Five minutes with Docker.
A shared network lets the containers talk by name. JetStream is enabled with -js.
docker network create nuts-demo
docker run -d --name nats --network nuts-demo \
-p 4222:4222 nats:2.12-alpine -js
Use nats-box as a one-shot CLI on the same network.
docker run --rm --network nuts-demo natsio/nats-box:0.19.0 \
nats -s nats://nats:4222 stream add EVENTS \
--subjects "events.>" --storage file --retention limits \
--max-msgs 10000 --max-age 24h --discard old --defaults
Save this as Caddyfile in your working directory:
:8080 {
route /events* {
nuts {
nats_url nats://nats:4222
stream_name EVENTS
topic_prefix events.
}
}
}
Then run NUTS, mounting that file:
docker run -d --name nuts --network nuts-demo \
-p 8080:8080 \
-v $(pwd)/Caddyfile:/app/Caddyfile:ro \
idcttech/nuts:latest
Save this as demo.html and open it in a browser:
<!doctype html>
<html>
<body>
<h1>NUTS demo</h1>
<ul id="messages"></ul>
<script>
const es = new EventSource(
'http://localhost:8080/events?topic=demo'
);
es.addEventListener('message', (e) => {
const data = JSON.parse(e.data);
const li = document.createElement('li');
li.textContent = data.topic + ': ' + JSON.stringify(data.payload);
document.getElementById('messages').appendChild(li);
});
</script>
</body>
</html>
Each nats pub shows up live in the browser.
docker run --rm --network nuts-demo natsio/nats-box:0.19.0 \
nats -s nats://nats:4222 pub events.demo '{"hello": "world"}'
docker run --rm --network nuts-demo natsio/nats-box:0.19.0 \
nats -s nats://nats:4222 pub events.demo '{"order": 42, "status": "shipped"}'
Build Caddy with NUTS using xcaddy:
xcaddy build --with github.com/ideaconnect/nuts
Or run the prebuilt image against a NATS server you already have:
docker run -d -p 8080:8080 \
-e NATS_URL=nats://host.docker.internal:4222 \
-v ./Caddyfile:/app/Caddyfile:ro \
idcttech/nuts:latest
Next: building and running for every option, and the configuration reference for every directive.
NUTS is inspired by Mercure, built for teams already running NATS. This site runs on it too: the live updates in the blog and the admin panel arrive through NUTS.