NATS server with JetStream, token auth, WebSocket, and HTTP monitoring as a single Dokploy Compose service.
flowchart LR
apps[Apps / Clients]
web[Browser / WS clients]
admin([Operator])
subgraph nats[NATS]
srv[nats-server]
js[(JetStream)]
srv --> js
end
apps -->|TCP 4222| srv
web -->|WSS via Traefik :8080| srv
admin -->|HTTP :8222 monitoring| srv
-
Create Service → Compose
- Provider: Git
- Repository: this repo (or your fork)
- Branch:
main - Compose path:
docker-compose.yml
-
Environment — paste
.env.exampleinto the Environment tab and set a strongNATS_AUTH_TOKEN:openssl rand -hex 32
-
Domains — open the Domains tab and add each entry below.
Host Path Service Container Port nats-monitor.<your-domain>/nats8222nats-ws.<your-domain>/nats8080nats-monitorexposes/healthz,/varz,/connz,/jsz, etc.nats-wsis the WebSocket endpoint (wss://nats-ws.<your-domain>).
-
Protect the monitoring endpoint with basic auth (Traefik middleware)
a. Generate a hashed credential
htpasswd -nb admin 'password' # → admin:$apr1$G3T3XOqn$6JGifVcvveyWFg7gYWZjH0
b. Create the middleware in Dokploy: go to Dokploy → Settings → Traefik and open the dynamic config file editor. Add or append to
middlewares.yml:http: middlewares: nats-monitor-auth: basicAuth: users: - "admin:$apr1$G3T3XOqn$6JGifVcvveyWFg7gYWZjH0"
c. Attach it to the
nats-monitor.<your-domain>row in the service's Domains tab:nats-monitor-auth@file -
Native protocol (port 4222) — Traefik routes HTTP, not raw TCP. To expose 4222 to outside clients, either:
- Add
ports: ["4222:4222"]to thenatsservice and open the firewall, or - Use the WebSocket endpoint from clients that support it.
- Add
- Node example — Fastify UI + worker, request/reply, WebSocket live events, prefix-scoped subscriptions