08 · Events & WebSocket
Status: Implemented (T10) · Owner: Platform
Everything that happens in Flux (a run changes state, a node finishes, a dataset commits, an artifact is published) is an event on NATS JetStream (ADR-0005). Events drive the live UI, the run state the control plane keeps, trigger rules (design 09) and notifications.
Envelope
Section titled “Envelope”CloudEvents 1.0-shaped JSON, identical from Rust (flux-events) and Python
(flux_cp.events):
{ "specversion": "1.0", "id": "5b0e…", "type": "flux.run.status", "source": "flux-runner/r-8f2c/0", "subject": "flux.run.r-8f2c.status", "time": "2026-09-30T10:00:00.123Z", "data": {"status": "running"}}id is also the JetStream Nats-Msg-Id, so a producer that retries within the
duplicate window (2 minutes) stores the event once. Publishing waits for the JetStream
ack, so a publish to a subject no stream covers fails instead of being dropped.
Types and subjects
Section titled “Types and subjects”Subject tokens (run ids, dataset names, rule names) must match [A-Za-z0-9_-]{1,128};
anything else is rejected, so a name can’t inject ., * or > into a subject.
| Type | Subject | Data (main fields) |
|---|---|---|
flux.run.status |
flux.run.<run>.status |
status (queued, running, succeeded, failed, cancelled), error |
flux.run.node |
flux.run.<run>.node |
node, state, rows, error |
flux.run.progress |
flux.run.<run>.progress |
shard, rows per node:port, elapsed_ms |
flux.run.log |
flux.run.<run>.log |
shard, level, message, node |
flux.dataset.committed |
flux.dataset.<name>.committed |
dataset, snapshot_id, rows, files, run_id |
flux.trigger.fired |
flux.trigger.<rule>.fired |
rule, pipeline, run_id |
flux.forge.published / revoked |
flux.forge.published / revoked |
name, version, platform, sha256, actor, reason |
flux.system.* |
flux.system.<service>.<what> |
e.g. flux.system.api.started |
Run, dataset and trigger producers arrive with T13–T15; Forge and the API publish now.
Streams
Section titled “Streams”The control plane creates or updates the streams on start (idempotent); it is the single owner of their configuration. All use file storage and discard old messages when full.
| Stream | Subjects | Retention | Indexed in Postgres |
|---|---|---|---|
FLUX_RUNS |
flux.run.*.status, .node, .progress |
14 days, 4 GiB | status, node |
FLUX_LOGS |
flux.run.*.log |
3 days, 4 GiB | no |
FLUX_DATASETS |
flux.dataset.> |
30 days | yes |
FLUX_TRIGGERS |
flux.trigger.> |
30 days | yes |
FLUX_FORGE |
flux.forge.> |
365 days | yes |
FLUX_SYSTEM |
flux.system.> |
14 days | yes |
Logs have their own stream so high-volume output can’t push status history out.
Control plane
Section titled “Control plane”Startup. With FLUX_EVENTS_ENABLED (default on), the API connects to NATS, ensures
the streams, starts the indexer and publishes flux.system.api.started. If NATS isn’t
reachable the API doesn’t start: running without it would silently miss run state
and triggers.
Indexer. Durable pull consumers (flux-api-index-*, explicit ack, 20 deliveries)
copy the indexed subjects into the event_log table (idempotent on event id), then run
registered handlers (@events.on("flux.run."), called with the event and the
database session) in the same transaction. A failure
naks the batch for redelivery after 5 s, so a database outage delays indexing without
losing events. Progress and logs are not indexed; the UI reads them live.
History API. GET /api/events?type=&run_id=&dataset=&before=&limit= (viewer;
flux.system.* only for admins) and GET /api/events/streams.
WebSocket gateway
Section titled “WebSocket gateway”/ws on the API host. JSON text frames:
client → {"op":"auth","token":"…"} only without a session cookieserver → {"op":"ready","subject":"…","role":"…"}client → {"op":"subscribe","id":"s1","topic":"run:r-8f2c"}server → {"op":"subscribed","id":"s1","topic":"run:r-8f2c"}server → {"op":"event","sub":"s1","event":{…}}client → {"op":"unsubscribe","id":"s1"} {"op":"ping"}server → {"op":"error","id":"s1","message":"…"} {"op":"pong"}Topics, not subjects: the browser never sends a NATS subject.
| Topic | Delivers | Replay |
|---|---|---|
runs |
status of every run | new only |
run:<id> |
status, node and progress of one run | full retained history, then live |
run:<id>:log |
logs of one run | full retained history, then live |
datasets, dataset:<name> |
commits | new only |
triggers, forge |
firings, artifact changes | new only |
system |
operational events (admins) | new only |
Opening a run page therefore shows everything that happened so far, even mid-run. Each subscription is an ephemeral ordered consumer; NATS removes it a minute after the socket goes away.
Security and limits
- Auth by the
flux_sessioncookie or a bearer token in the first frame (10 s). Tokens never go in the URL, where they’d end up in logs. - The
Originmust be an allowed origin or the API’s own host (cookies would otherwise allow cross-site WebSocket hijacking); others are refused with4403. - Per-topic role checks, at most 64 subscriptions and 4 KiB frames per socket.
- The socket closes with
4401when the token expires. - Each socket has a 1000-message send queue. A client that can’t keep up is closed with
4008, instead of the API buffering without bound; it reconnects, and run topics replay.
Browser client (frontend/src/events.ts): one shared socket per tab,
useEvents(topic, handler) and useLiveStatus(). It subscribes after ready,
re-subscribes after reconnecting with exponential backoff (1–30 s; 30 s after 4401),
and stops reconnecting when nothing is subscribed. Replays can repeat events, so
handlers dedupe by event.id where it matters. Forge pages refresh on forge events;
the header shows the live status.
Rust producers
Section titled “Rust producers”flux-events provides Event, subject builders with token validation,
NatsPublisher (waits for the ack) and MemPublisher for tests. flux-forge publishes
flux.forge.published / revoked when FLUX_NATS_URL is set. Forge’s database stays
the record for artifacts, so a failed publish there is logged rather than failing the
request.
Tested
Section titled “Tested”- Rust: subject injection rejected, envelope shape, publish with ack and dedupe, and failure when no stream covers a subject (real NATS); Forge emits one event per change.
- Control plane (real NATS, isolated subject root and stream prefix): indexing and history, progress not indexed, dedupe, admin-only system events, origin and auth rejection, replay then live delivery for run topics, topic validation and roles, bearer auth, live dataset topics, unsubscribe.
- Browser client: subscribe after ready, routing, reconnect and re-subscribe, backoff
after
4401, no reconnect without subscriptions.
Open questions
Section titled “Open questions”- Multi-tenant subjects (
flux.<tenant>.run…) when tenants arrive. - Sampling progress events for very wide pipelines; producers throttle progress (T13).
- One NATS consumer per browser subscription is fine for hundreds of sockets; a shared fan-out would be needed for thousands.
See the architecture overview.