Setup: WebSocket, With Credentials
WebSocket, but clients must authenticate — also enables System Service ($SYS.>) monitoring.
1. NATS server setup
# Enable authentication with username and password.
listen: 0.0.0.0:4222
http_port: 8222
# WebSocket transport — required for the browser to connect at all.
websocket {
port: 8080
no_tls: true
}
# JetStream — required for KV buckets and Streams.
jetstream {
store_dir: "/data"
}
# Accounts + system account — required to receive $SYS.> events.
# JetStream cannot be enabled on the system account, so it lives only in
# APP; SYS exports its $SYS.> events to APP under a "sys." prefix so a
# single connection (user "joes") sees everything.
system_account: SYS
accounts {
SYS: {
users: [
{ user: sysuser, password: "adminpassword" }
]
exports: [
{ stream: "$SYS.>", accounts: [APP] }
]
}
APP: {
jetstream: enabled
users: [
{ user: joes, password: "mypassword" }
]
imports: [
{ stream: { account: SYS, subject: "$SYS.>" }, prefix: "sys" }
]
}
}
Run NATS server Docker
docker run -d --name nats-server \
-p 4222:4222 -p 8222:8222 -p 8080:8080 \
-v $(pwd)/nats-server.conf:/etc/nats/nats-server.conf:ro \
nats:latest \
-c /etc/nats/nats-server.conf
One-time nats-cli connection setup
nats context add tutorial-with-auth \
--server="nats://10.135.32.227:4222" \
--user="joes" --password="mypassword" \
--selectIn Connection section, enter:
- NATS server host
- 10.135.32.227
- WS port
- 8080
- TLS (wss)
- off
- Username
- joes
- Password
- mypassword
2. Subscription to subject / KV bucket / stream
In the Subscriptions form: Type: Subject · Target: orders.>
No setup required — just add the subscription above.
In the Subscriptions form: Type: KV Bucket · Target: my-bucket
nats kv add my-bucketIn the Subscriptions form: Type: JetStream Stream · Target: ORDERS · Filter: orders.created (optional)
nats stream add ORDERS --subjects "orders.>" --storage file \
--retention limits --max-msgs=-1 --max-bytes=-1 --max-age=0 --defaultsIn the Subscriptions form: Type: System Service · Target: sys.$SYS.>
No setup required — just add the subscription above.
No client-side setup needed — the SYS → APP export/import in nats-server.conf above already forwards every $SYS.> event to the "joes" user under a "sys." prefix.
3. Messages creation
nats pub orders.created '{"id": 1, "total": 42.5}'nats kv put my-bucket hello worldnats pub orders.created '{"id": 1}'# any client activity produces $SYS events, e.g. a connect/disconnect:
nats pub foo bar
# to see the RAW (unprefixed) $SYS.> feed directly, connect as the sys user instead:
nats sub -s "nats://sysuser:[email protected]:4222" "\$SYS.>"In the viewer, subscribe to sys.$SYS.> — not the raw $SYS.> — since that’s the prefix APP imports it under.