Learn · Data & files
Live queries & presence
A live query keeps a result current as commits change the rows, under the same access rules as any read. Presence shows who else is on a page.
- CLI
napalm watch- In the schema
- —
Subscriptions and streams#
A subscription keeps one read live. The client receives the full result first and then a patch each time a commit changes the result.
Multiplexed SSE conduit
One long-lived POST /subscribe connection multiplexes active read subscriptions and dynamic POST /subscribe/{stream} updates across your app.
Commit-cut patch wavefronts
At each commit step, affected subscriptions re-evaluate on one shared snapshot under caller policies and emit a single atomic ID-keyed patch frame.
Pass name to subscribe to a deployed query from any session or service key. Callers admitted by the project’s passthrough can also send an EdgeQL statement in query, and other callers receive deployed_query_required.
Only reads stay live, and a statement that writes returns query_rejected. Deployed queries attached to an action handler send rows only to that handler and return action_not_live.
One Server-Sent Events stream from POST /subscribe multiplexes many subscriptions, and POST /subscribe/{stream} adds or drops subscriptions while connected. Client libraries share a single stream across the app, and napalm watch streams a query in the terminal using a service key.
The Live queries reference documents every request and frame. Realtime and collaboration builds a shared screen on live queries.
The path of a commit#
A commit reaches an open subscription through six steps.
- Every commit
- A stream observes every commit on its branch in commit order.
- Batching
- Commits that land close together coalesce into one step.
- Reads
- Each subscription the commit may affect runs again, and every run in one step reads the same snapshot.
- One frame
- Each stream receives a single
patchframe for the step carrying every subscription that moved, so lists and detail views on one screen advance together. - Patch by id
- A patch carries new objects whole, changed fields on modified objects and removed object ids. When rows have no ids or the full result is smaller, the full result is sent instead.
- Heartbeat
- Each stream receives a
heartbeatframe every 15 seconds so proxies keep quiet streams open.
Access rules on every result#
Every step runs as the caller that opened the stream under that caller’s globals and the schema’s access policies. Rows the caller cannot read never reach the stream, and a commit that revokes access removes the affected rows in that commit’s patch.
Between commits, open streams also react immediately to credential and project changes.
- Keys
- A revoked or expired key ends the key’s streams within a second with
key_revoked. - Sessions
- Signing out, revoking a session or deleting the person’s identity ends open streams with
session_ended. - Globals
- Updated endpoint globals re-run every subscription and send a fresh
resultframe. - Passthrough
- A passthrough that stops admitting the caller drops the caller’s own statements with
deployed_query_required. - Deploys
- Redeploying a query moves active subscriptions to the updated statement. Removing a deployed query returns
not_found, and attaching an action handler returnsaction_not_live. - Schema pushes
- A schema push runs every subscription again. A statement the new schema breaks drops the subscription with
schema_changed.
Bookmarks and freshness#
Every result and patch frame carries a commit position as lsn. An lsn works anywhere a bookmark (Napalm-Bookmark) works.
Causal bookmark pin
Passing a write’s Napalm-Bookmark or prior frame lsn starts or resumes live subscriptions at or past that commit with zero missed mutations.
Every write returns a Napalm-Bookmark header. Pass that value as lsn when opening a stream, or in Napalm-Bookmark on POST /subscribe/{stream}, to start at or past the write. Client libraries forward the latest bookmark automatically so a subscription opened after a write never misses that write. Each step reads at or past the triggering commit and never precedes an earlier frame, so results never move backward.
Unlike an unbookmarked read, a live step bypasses the engine’s in-memory cache and reads PostgreSQL directly. Because a write’s HTTP response can arrive slightly before the matching patch frame, Realtime & collaboration holds optimistic edits on screen until the patch arrives.
The project’s subscription limit#
A project holds up to 1,000 subscriptions at once for each GiB of memory across the cluster’s engines, and at most 100,000 subscriptions. A cluster with two 2 GiB engines holds four times that first figure. Adding engines or engine memory raises the limit, and no software setting overrides it. The count covers every stream on every engine and every branch, so a preview branch’s live queries share main’s limit.
- A subscription on a stream
- One subscription.
- A joined room
- One subscription.
- A stream’s watched rooms
- One subscription for each object type.
Opening a stream or adding subscriptions past the limit answers 429 live_limit_reached, and nothing is added. The client libraries do not retry live_limit_reached, so close an unused subscription first. GET /projects/{id}/live returns a branch’s open streams along with the project’s count and limit.
A subscription’s result holds up to 16 MiB. A larger result drops the subscription with result_too_large.
Off-screen subscriptions#
A subscription for hidden content still counts toward the limit and still runs on every commit that moves the result. Close the subscription while the content is hidden.
- TypeScript
- Pass
{ isPaused: true }to a live query hook. The hook closes the subscription and keeps the last rows.usePresencetakesisPausedtoo and leaves the room while paused. The React Native guide pauses a list in the background. - Dart
- Cancel the
StreamSubscription. The client closes the stream once the last subscription is cancelled. The Flutter guide pauses a feed while the app is in the background.
A resumed subscription opens again from the client’s bookmark, so the first result is never older than the rows on screen.
Ended streams#
An error frame that names a subscription drops that subscription alone while keeping the stream open. The client libraries re-add a subscription dropped with project_unavailable after a short backoff and surface every other code to the caller. An error frame without a subscription closes the entire stream.
| Code | Why the stream ended | Client libraries |
|---|---|---|
key_revoked | The stream’s service key was revoked or expired. | Client librariesStop and report the error. |
session_ended | The session was signed out or revoked. A deleted identity ends the session’s streams too. | Client librariesStop and report the error. |
api_off | The project’s API was turned off with napalm endpoint --disable. | Client librariesStop and report the error. |
engine_stopping | The engine holding the stream is shutting down. | Client librariesReconnect. |
too_slow | The client left more than 16 MiB of frames unread. | Client librariesReconnect. |
project_unavailable | The engine could not read the subscriptions momentarily, or a branch move closed open streams. | Client librariesReconnect. |
Opening a stream can also answer 503 live_unavailable while the branch cannot stream changes yet. The client libraries retry the request automatically.
The client libraries reconnect when a connection drops or when no frame arrives for 35 seconds. A reconnect opens a new stream with the last lsn, and the new stream answers one whole result at least that recent without replaying intermediate changes one by one.
A session stream sends token_expired thirty seconds before the bearer token expires, and the client reconnects immediately using a refreshed token without losing rows. Session renewal and sign-out explains session tokens.
Presence rooms#
Every object has an in-memory room keyed by UUID. Streams join the room to share small JSON states and ephemeral messages without writing to PostgreSQL.
Policy-gated in-memory presence rooms
Streams authorized by the object's select policy share ephemeral JSON state and messages in memory without writing to PostgreSQL, evicting members when read access ends.
Joining reads the object as the stream’s caller, so any caller permitted to read the object may join. The member leaves at the commit that revokes read access. An unreadable object and an unknown id both return not_found, so presence rooms never reveal which objects exist.
A member is one stream’s place in a room, so two tabs are two members of one person. Watching a room gives the number of people and the first 5 people without joining. A watcher sees no states or messages, and members never see the watcher.
A disconnected member holds a spot in the room for 10 seconds so the client can rejoin and receive missed messages. Backend servers can read members or send messages without opening a stream through GET /presence/{object} and POST /presence/{object} when authorized to read the object.
- State
- Up to 4 KiB of JSON. A larger state answers
413 presence_too_large. - Message
- Up to 16 KiB of JSON. A larger message answers
413 presence_too_large. - Rooms joined
- 16 per stream. Exceeding the limit answers
429 presence_limit_reached. - Rooms watched
- 256 per stream. Exceeding the limit answers
429 presence_limit_reached. - Members
- 200 in each room. A join past the limit leaves the room with
channel_full. - Messages
- 32 a second from each member, and messages past the rate are dropped. A server past the rate gets
429 presence_rate_reached. - Pace
- A room sends at most one frame every 50 ms. States set in between collapse to the latest state.
The Live queries reference gives the room’s requests and frames. Realtime & collaboration shows a viewer list with usePresence.