skip to content
szczecin 53.43N 14.55E ./start-project
nuts 0.6.1 Caddy module · open source · free

bartosz@idct:~/software/nuts$ cat QUICKSTART.md

quick start

Spin up NATS, create a stream, run NUTS, open a page and watch messages arrive in real time. Five minutes with Docker.

Create a Docker network and launch NATS

A shared network lets the containers talk by name. JetStream is enabled with -js.

sh
docker network create nuts-demo

docker run -d --name nats --network nuts-demo \
  -p 4222:4222 nats:2.12-alpine -js

Create a JetStream stream

Use nats-box as a one-shot CLI on the same network.

sh
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

Write a Caddyfile and launch NUTS

Save this as Caddyfile in your working directory:

caddyfile
:8080 {
  route /events* {
    nuts {
      nats_url    nats://nats:4222
      stream_name EVENTS
      topic_prefix events.
    }
  }
}

Then run NUTS, mounting that file:

sh
docker run -d --name nuts --network nuts-demo \
  -p 8080:8080 \
  -v $(pwd)/Caddyfile:/app/Caddyfile:ro \
  idcttech/nuts:latest

Drop in a tiny HTML page

Save this as demo.html and open it in a browser:

html
<!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>

Publish a few messages

Each nats pub shows up live in the browser.

sh
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"}'

Install it your way

Build Caddy with NUTS using xcaddy:

sh
xcaddy build --with github.com/ideaconnect/nuts

Or run the prebuilt image against a NATS server you already have:

sh
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.