monoblok docs
Docs menu · NATS clients, auth and HTTP

Server

NATS clients, auth and HTTP

monoblok speaks enough of the NATS wire protocol that any NATS client can connect. You can drop it in alongside existing tooling without writing a client library first.

NATS support#

monoblok implements the NATS core pieces it needs to behave like a small broker.

FeatureSupport
PUB / SUB / UNSUB / MSGyes
wildcardsyes
request/reply / reply-toyes, local core reply-to; bridge remains export-only
queue groupsyes
headersno
$LVC.* last-value replayyes, monoblok extension
$STATS.* live countersyes, monoblok extension
bridge to real NATSexport-only
import from real NATSyes, as private patchbay ingress
JetStream importyes, as consumer-only patchbay ingress
TLS on the local serveryes, optional server cert/key
auth on the local serveryes, optional global token or user/pass from environment
HTTP/SSE adapteryes, optional plain HTTP listener
JetStream service for local clientsno
clusteringno

Request-reply works through patchbay as well as plain pub/sub. A rule can read the request's reply subject as reply-to, and (reply! VALUE) publishes to it, so (on "q.temp" (reply! (last "temp.kitchen"))) makes nats req q.temp "" answer with the latest cached kitchen temperature. reply-to is nil for imported messages, clock ticks, and every bodies. See Request-reply.

Without auth flags, CONNECT bodies are accepted and ignored. +OK is never sent. Client publishes to $LVC.* and $STATS.* are rejected, and while an import form is configured all client PUB commands are rejected.

Simple auth for local NATS clients#

Local client auth is optional and global. Configure either a token or a single username/password pair by naming the environment variables that hold the expected secret values:

shell
MONOBLOK_AUTH_TOKEN='sekret' \
  monoblok --port 4222 --patchbay patchbay.edn \
    --auth-token-env MONOBLOK_AUTH_TOKEN

MONOBLOK_AUTH_USER='alice' MONOBLOK_AUTH_PASS='wonder' \
  monoblok --port 4222 --patchbay patchbay.edn \
    --auth-user-env MONOBLOK_AUTH_USER \
    --auth-pass-env MONOBLOK_AUTH_PASS

With auth enabled, INFO advertises "auth_required":true. Clients must send the normal NATS CONNECT fields: auth_token for token auth, or user and pass for username/password auth. Missing or wrong credentials receive -ERR 'Authorization Violation' and the socket is closed.

Authentication is checked at CONNECT time. It does not add per-subject authorisation, accounts, bcrypt hashes, JWTs, NKEYs, or reloadable user maps. Use TLS, a trusted network, or a fronting proxy if credentials cross anything outside your control.

TLS for local NATS clients#

Server-side TLS is optional. Start monoblok with a PEM certificate chain and matching private key:

shell
monoblok --port 4222 --patchbay patchbay.edn \
  --tls-cert /etc/monoblok/server.crt \
  --tls-key /etc/monoblok/server.key

monoblok follows the normal NATS TLS upgrade flow: the accepted socket first receives a plaintext INFO containing "tls_required":true, then the client starts TLS on that same socket before sending CONNECT. Non-TLS clients will not be able to complete the connection once TLS is enabled.

Use a certificate trusted by your clients in production. For private/self-signed certificates, configure clients with the CA certificate where possible. Test clients can disable verification, but that is insecure and should stay out of production. With nats.c, the development-only equivalent is natsOptions_SkipServerVerification(opts, true).

HTTP/SSE adapter#

The optional HTTP listener is a small browser-friendly adapter over the same local client publish and subscribe paths. It is disabled by default:

shell
monoblok --port 4222 --patchbay patchbay.edn \
  --http-host 127.0.0.1 \
  --http-port 8080

Routes use path segments as NATS subject tokens:

text
GET  /sub/sensors/temp       -> SSE subscription to sensors.temp
GET  /sub/sensors/%3E        -> SSE subscription to sensors.>
GET  /sub/$LVC/sensors/temp  -> SSE subscription to $LVC.sensors.temp
GET  /latest/sensors/temp    -> one-shot LVC lookup for sensors.temp
POST /pub/sensors/temp       -> client publish to sensors.temp

GET /latest/<subject> returns the cached payload for that exact, unprefixed subject as Content-Type: text/plain; charset=utf-8, with no JSON wrapper. It returns 404 when LVC has no value for the subject and 409 when LVC is disabled. Use /sub/$LVC/... for wildcard replay or for a stream that stays open for future updates.

POST requires Content-Length plus Content-Type: text/plain or Content-Type: application/json, and rejects NUL bytes. It is intentionally a text adapter, not a binary payload API. Successful POSTs fan out exactly like a local NATS PUB: normal subscribers, SSE subscribers, LVC, bridge, and patchbay all see the same publish.

SSE messages are JSON data envelopes:

text
event: msg
data: {"subject":"sensors.temp","payload":"31.2"}

When local auth is configured, HTTP uses Authorization: Bearer <token> for token mode or Authorization: Basic ... for user/pass mode. The HTTP listener is plain HTTP only; put Caddy, nginx, or another proxy in front for HTTPS. A tiny fetch-based JavaScript helper lives at examples/http-sse-client.js, with a browser page at examples/http-sse-client.html.

Every HTTP response carries a Server: monoblok/v<version> header.

nginx reverse proxy for SSE#

Run monoblok's HTTP listener on loopback and let nginx terminate public TLS:

shell
monoblok --port 4222 --patchbay patchbay.edn \
  --http-host 127.0.0.1 \
  --http-port 8080

Use a proxy_pass without a URI suffix so encoded subject tokens such as %3E stay intact. SSE subscriptions also need buffering and compression disabled, with a read timeout longer than the longest expected quiet period:

nginx
upstream monoblok_http {
    server 127.0.0.1:8080;
}

server {
    listen 443 ssl;
    server_name events.example.com;

    ssl_certificate     /etc/letsencrypt/live/events.example.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/events.example.com/privkey.pem;

    location /sub/ {
        proxy_pass http://monoblok_http;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "";

        proxy_buffering off;
        proxy_cache off;
        gzip off;
        proxy_read_timeout 1h;
        proxy_send_timeout 1h;
    }

    location /pub/ {
        client_max_body_size 1m;

        proxy_pass http://monoblok_http;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "";
    }

    location /latest/ {
        proxy_pass http://monoblok_http;
        proxy_http_version 1.1;
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_set_header Connection "";
    }
}

Serve browser clients from the same nginx origin when possible. monoblok does not add CORS headers itself, and same-origin /sub/..., /pub/..., and /latest/... requests avoid needing browser-specific CORS handling. If monoblok auth is enabled, make sure nginx forwards the Authorization header; the default proxy behaviour does unless you override it.

Use the Node demo server to serve the page and proxy /sub, /pub, and /latest from the same origin, which avoids browser CORS. It can also start a local monoblok process for the demo:

shell
node examples/http-sse-demo.js --start-monoblok --port 8090 --open

MIT licensed · © 2026 Alex Reid
Lexvica offers solutions built with monoblok.