Skip to content

Realtime (SSE)

Realtime pushes use Server-Sent Events in one direction only. Both endpoints authenticate with a session sid but need different permissions, notification:self and realtime:self.

EndpointWhat it pushes
POST /api/v1/notifications/streamunread notification count
POST /api/v1/realtime/streamgeneric event envelopes with channel subscriptions

Both are POST, so EventSource is unusable — use fetch and read response.body, which is what @feedchamber/sse-client does.

终端窗口
curl -N -X POST http://localhost:8080/api/v1/notifications/stream \
-H "Authorization: Bearer $TOKEN" -H 'Accept: text/event-stream'

You get the current unread count immediately, then one push per change:

data: {"unread":3}
: keepalive
data: {"unread":4}

The field is unread, unlike the {"count":n} from POST /notifications/unread_count — keep two parsers.

An idle stream emits no bytes, so any intermediary with a read timeout cuts it (nginx defaults proxy_read_timeout to 60 s). The handler therefore writes the comment : keepalive every 15 seconds; clients must ignore comment lines, and the frontend parser only reacts to data: lines, so it can never surface as an empty event. If the KV subscription fails (say NATS is briefly down) the endpoint degrades to polling every sse_polling_interval seconds (default 5) instead of dropping the connection.

终端窗口
curl -N -X POST http://localhost:8080/api/v1/realtime/stream \
-H "Authorization: Bearer $TOKEN" -H 'Accept: text/event-stream' \
-H 'Content-Type: application/json' -d '{"channels":["notif:01J…"]}'

Only your own channel notif:<your user id> is accepted; other names are silently dropped, and if none survive you fall back to your own. Events carry an id:, e.g. id: 8 followed by data: {"type":"message","channel":"heartbeat","payload":{"ts":1793000000},"id":8}.

SSEClient options include url, method (default POST), withCredentials, refreshToken, onData, maxRetries (default 5) and resumeOnVisible. Backoff is exponential — 3 s, ×1.5, capped at 30 s — and resets on any successful connection. On a 401 it calls refreshToken() once: success reconnects, failure clears local auth state and stops.

const client = new SSEClient({
url: '/api/v1/notifications/stream',
method: 'POST',
withCredentials: true, // HttpOnly cookie auth; JS cannot read the token
refreshToken: () => tryRefreshToken(),
onData: (d) => typeof d.unread === 'number' && setUnread(d.unread),
});
client.start(); // stop() on unmount

nginx’s default proxy_read_timeout cuts an idle stream at roughly 61 s, so the browser reconnects — a hiccup every minute. Deployments give both streams their own location:

location ~ "^/api/v1/(notifications|realtime)/stream$" {
proxy_pass http://backend:8080;
proxy_http_version 1.1;
proxy_set_header Connection "";
proxy_buffering off;
proxy_cache off;
proxy_read_timeout 3600s;
}

proxy_buffering off matters as much as the timeout: with buffering on, nginx accumulates small events and pushes arrive in batches. The regex location wins over the /api/v1/ prefix, and with the 15 s keepalive in place this block is a backstop, not the only mechanism.

The server does not limit connections per user and there is no cross-tab deduplication: three tabs mean three connections and three reconnection storms. Keep one per tab — subscribe at the layout level, stop() on unmount, pause background tabs with resumeOnVisible. For a single site-wide connection, elect a leader tab and broadcast over BroadcastChannel.

Connect with curl -sN --max-time 3: the unread count arrives at once, a reply to your post pushes a second frame, and a : keepalive appears after 15 idle seconds. The feedchamber_sse_connections{channel="notification"} gauge should drop back on disconnect.

SymptomCauseFix
drops every ~60 sproxy read timeout or missing keepalivecheck the SSE location and buffering
immediate 401 / 403missing credential or permissionsend the cookie or Bearer
events arrive in batchesa buffering intermediarydisable proxy_buffering
gives up after retriesmaxRetries reachedcheck NATS, or raise the cap