Realtime sessions

What a session on a document is, how it starts and ends, and how clients stay in sync across reconnects.

A session is one live connection to one document. While a session is open, the server sends it every change to the document as it is committed, and accepts the session's own edits. This page describes a session's life as an application sees it. The client in @voidhash/mimic/sync does all of it for you; see Creating a client.

Opening a session

A client opens a WebSocket at the server's /v1/sessions endpoint, for example wss://db.example.com/v1/sessions. Its first message names the database, the collection and the document, and carries a credential: a document token for that document, or a database key. The server loads nothing of the document until the credential is verified, and closes a socket that does not authenticate within 10 seconds.

  • A session whose credential holds write can submit transactions.
  • A read session receives the document, its changes and other participants' presence, and may publish presence of its own. Its edits are refused, so the client holds the document read-only.

Once the credential verifies, the server answers with the document's revision, the session's permission, and when its credential expires. It then brings the client to the head of the document: either the changes after the revision the client already holds, or, when the client holds nothing or its copy cannot be resumed, the document's current state. From then on it sends each committed change in order.

A session may open on a document that does not exist yet. The client reads it as absent until it is created, by this client or another.

Staying in sync

Changes arrive in the order the server committed them, each with the document's revision after it. A client applies them to its copy of the server's document, so the copy is always the document at some revision.

State hashes

Every change and every state the server sends carries a hash of the document's content. The client computes the same hash over its own copy and compares. On a mismatch it discards its copy and opens the session again, so a client can never silently drift from the server. Three mismatches in a row stop syncing the document with a data_corruption sync error: a defect that reopening cannot fix.

Reconnecting

Connections drop. The client re-establishes a lost session automatically: the first attempt within a quarter of a second, later ones backing off from 1 second up to 30 seconds. It treats a session as lost when the socket closes, when the server does not answer its pings, or when an answer it waits for is 45 seconds late.

The new session resumes from the revision the client holds: the server sends the changes after it, verified by the state hash, so nothing is lost or applied twice. When those changes are no longer kept, or the document's history was replaced (a restore), the server sends the current state instead, and the client tells your application through onReplaced.

The client then sends again, one at a time and in order, the edits the server has not answered. Each edit keeps the transaction id it was made with, and the server evaluates an id at most once, so an edit that reached the server before the connection dropped is answered with its recorded outcome rather than applied a second time. Edits made while offline are sent the same way when the connection returns.

Credentials and expiry

The client asks your application for a credential (a function you pass it) every time it opens a session. A document token can be used again until it expires, so the function may return the one it returned before.

Before a token expires, the client asks for a new one and renews the session with it, without reconnecting: once 80% of its lifetime has passed, or when 30 seconds remain. A session whose token expires anyway is closed unauthenticated, and the client opens it again with a new one. Only when two credentials in a row are refused does the client stop and report a terminal sync error.

When a session ends for good

Most closes are temporary, and the client reopens with backoff. Some are terminal: the client stops syncing the document and reports a MimicSyncError with terminal set. Pending edits are kept in every case.

CodeWhat happened
unauthenticatedTwo credentials in a row were refused
permission_deniedThe credential does not hold read on the document (or a database was blocked)
not_foundThe collection is gone, the token names another document, or the document is not visible to it
migration_failedThe document could not be migrated to its collection's new schema; an operator must repair it
schema_mismatchThe document's schema version is outside the versions this client declared it reads
unsupported_versionThe server does not speak this client's protocol version; the client needs updating
data_corruptionThe client's copy and the server's disagreed three times in a row
invalid_argumentThe client sent something the server could not read: a defect, reported

unavailable, resource_exhausted and internal closes, a server that restarts, and a lost network are temporary.

Quiet sessions

A session that has seen no change for a minute is parked by the server: it stays open and the client notices nothing, but the document no longer needs to stay in memory for it. The next change, or the client's next edit, wakes it, and it receives what it missed. Applications need do nothing about this; it is why many idle open documents cost the server little.

Sessions from a backend

A backend service, such as an AI agent that edits alongside users, does not need a WebSocket. It writes over the HTTP API with the engine SDK, and can appear to the document's participants with a presence lease that it renews. Its edits carry the subject of the token it wrote with, so clients can tell them apart. See Transactions from a backend.

Limits

A database's limits can bound how many sessions are open at once (connections) and how much they send and receive. A session the server cannot admit is refused resource_exhausted, and the client retries with backoff. Presence messages beyond 30 per second per session are dropped.

Next