Learn · Data & files
Live queries & presence
Keep query results synchronized as transactions commit under your read policies, and share ephemeral cursors and viewers in memory.
- 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 stream
One open SSE connection carries active subscriptions for your entire app.
Atomic commit patches
Affected subscriptions re-run against one snapshot and arrive together in one 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 affected by the commit runs again against the same database snapshot.
- One frame
- Each stream receives a single
patchframe per step containing every updated subscription, 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
- Revoking or expiring a service key closes its open 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
- Tightening passthrough drops ad-hoc subscriptions from excluded callers 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 re-evaluates every active subscription and drops any broken statement 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 bookmarks
Bookmarked streams start at or past your latest write without missing changes.
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 that cap. The count covers every stream on every engine and every branch, so a preview branch’s live queries share main’s limit.
- Read subscription
- Counts as one subscription on the stream.
- Joined presence room
- Counts as one subscription.
- Watched presence rooms
- Counts as one subscription for each watched object type.
Opening a stream or adding subscriptions past the limit returns 429 live_limit_reached without adding the subscription. 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 changes its rows. 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 briefly lost connectivity to the branch, or a branch operation closed open streams. | Client librariesReconnect. |
Opening a stream returns 503 live_unavailable if the branch is still starting up. Client libraries retry automatically.
Client libraries reconnect when a connection drops or when no frame arrives for 35 seconds. Reconnecting opens a new stream at the last lsn and delivers one fresh result frame rather than replaying missed patches 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.
In-memory presence rooms
Authorized viewers share live cursor or status JSON in memory without database writes.
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 returns
413 presence_too_large. - Message
- Up to 16 KiB of JSON. A larger message returns
413 presence_too_large. - Rooms joined
- 16 per stream. Exceeding the limit returns
429 presence_limit_reached. - Rooms watched
- 256 per stream. Exceeding the limit returns
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 receives
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 documents presence requests and frames. Realtime & collaboration shows a viewer list with usePresence.