Events and replication · Engineering

Watch, long-poll, and live streams

Three ways to consume changes: bounded watch, continuous NDJSON/SSE streams, and connection-scoped live subscriptions.

NonoDB has three related ways to observe changes, and they solve different problems:

  • watch — a bounded long-poll over /v1/exec;
  • /v1/watch — a continuous NDJSON or SSE stream;
  • mode: "live" — a connection-scoped subscription that evaluates a condition in the writer.

Choosing the wrong one leads to either missed changes or unnecessary load.

Watch: a bounded long-poll

watch tails an append-only event collection such as _events:

{
"op": "watch",
"coll": "_events",
"after": "01J...",
"limit": 100,
"timeout_ms": 1000,
"types": ["order.paid"],
"filter": {"payload.total": {"$gte": 1000}}
}

It returns a batch, a next token, and flags:

{
"events": [],
"next": "01J...",
"timed_out": false,
"has_more": false,
"truncated": false,
"oldest": null
}

The request is a normal read, requires read access, is not cached, and is not allowed inside a read batch. Vault collections are rejected.

If has_more is true, call again with after: next. If false, keep the same cursor and long-poll for new events.

truncated means the server cannot confirm that after still exists; events may have expired. A consumer that needs no gaps should restart from oldest after a snapshot, not silently continue.

The continuous stream

POST /v1/watch uses the same body and returns:

  • application/x-ndjson by default;
  • text/event-stream when the client sends Accept: text/event-stream.
Terminal window
curl --unix-socket /run/nonodb/nonodb.sock \
-H 'content-type: application/json' \
-H 'accept: application/x-ndjson' \
-d '{"op":"watch","coll":"_events","after":"01J..."}' \
http://localhost/v1/watch

NDJSON frames:

{"stream":{"truncated":false,"oldest":null}}
{"_id":"01J...","type":"order.paid","payload":{"total":1200}}
{"heartbeat":true,"ts_ms":1762800001000}

SSE frames use event: ready, event: message, id:, and event: heartbeat.

Stream fields include heartbeat_ms, timeout_ms, overlap_ms, filter, projection, and limit.

Concurrent producers can commit a lower _id after a higher one. The stream rescans the last overlap_ms window, suppresses already-delivered ids in memory, and can deliver a lower id after a higher one. Event order is not global commit order; delivery remains at-least-once.

The bounded long-poll does not do this rescan. Use the continuous stream or the Python EventConsumer when producers run concurrently.

Live mode

mode: "live" does not tail _events. It registers an in-memory subscription for the lifetime of the HTTP connection, evaluates a condition in the writer, and pushes matching changes directly to the stream.

{
"op": "watch",
"coll": "orders",
"mode": "live",
"filter": {"status": "paid"},
"ops": ["insert", "update", "delete"],
"heartbeat_ms": 10000
}

Optional fields include count, once, name, event_type, heartbeat_ms, and timeout_ms.

Live events look like subscription events:

{
"_id": "01J...",
"type": "subscription.live",
"payload": {
"subscription": "live",
"coll": "orders",
"op": "update",
"before": {"_id": "o1", "status": "new"},
"after": {"_id": "o1", "status": "paid"}
}
}

Semantics:

  • the subscription exists only while the stream is connected;
  • closing the stream removes it immediately;
  • a janitor removes half-open connections after 30 seconds without a successful heartbeat;
  • live events are not written to _events, so they do not grow the database;
  • there is no replay: reconnect, run a snapshot/find, then open a new live stream;
  • slow consumers are disconnected instead of buffering without bound;
  • after, types, and threshold are rejected in live mode;
  • vault collections are rejected;
  • SQL writes, find_and_modify, TTL expiry, and normal writes trigger evaluation.

Python:

for event in db.collection("orders").watch_live(
filter={"status": "paid"},
heartbeat_ms=10_000,
):
print(event["payload"]["after"])

Which one to choose

Need Use
Process durable events with checkpoints watch + EventConsumer
Continuous tail with heartbeats /v1/watch NDJSON/SSE
UI updates for a condition while connected live mode
Replay after disconnect watch with a cursor
No event collection growth live mode
Cross-process durable delivery streams with consumer groups

Delivery guarantees

  • watch and streams are at-least-once for durable events.
  • Live mode is best-effort while connected; it has no replay.
  • Heartbeats are not events.
  • In the default tail, checkpoints belong to the consumer. Group mode stores offsets in the daemon; see the streams guide.
  • Stream truncation is explicit; do not ignore it.

The streams guide and subscriptions guide cover all three modes, client examples, and metrics.

All engineering notes →Read the docs