Presence
Sharing short-lived state such as names, cursors and selections between the participants of a document.
Presence is the state a participant shares with the others while it is connected: a name, a cursor position, a selection. It is not part of the document. It is never stored, it has no history, and it disappears when the participant's session ends. This page explains how presence works and how to use it from the client.
The model
Each session on a document has at most one presence value. The server keeps the last value of each session in memory and relays changes:
- When a session opens, it receives its own presence id and the last presence of every participant already there.
- When a participant sets or clears its presence, every other session is told.
- When a session ends, its presence is removed for everyone.
Any session that may read the document may publish presence, read
sessions included. A presence value is at most 4,096 bytes of JSON, and a
session publishes at most 30 values per second: the client sends only the
latest value at that rate, and the server drops what goes beyond it. A
document admits a bounded number of presence participants; a session beyond
it works normally but neither publishes nor sees presence.
Presence values are application data. The server checks only their size: any participant can publish any value, so do not trust a name in a presence value the way you trust your own backend's records.
Declaring presence
Presence has a schema, defined with the same primitives as a document. Pass
it to createSyncClientDocument:
import { Primitive } from "@voidhash/mimic-core";
import { createSyncClientDocument } from "@voidhash/mimic/sync";
const Presence = Primitive.Struct({
name: Primitive.String().required(),
cursor: Primitive.Struct({
x: Primitive.Number().required(),
y: Primitive.Number().required(),
}),
}).required();
const document = createSyncClientDocument({
primitive: BoardDocument,
url,
database,
collection,
document: documentId,
credential: fetchToken,
presence: { primitive: Presence, initial: { name: "Ada" } },
});initial is published as soon as the session opens. Without the presence
option, document.presence is undefined.
Using presence
const presence = document.presence;
// Publish this participant's state. It replaces the previous value.
presence?.set({ name: "Ada", cursor: { x: 120, y: 48 } });
// Read the others.
for (const [sessionId, entry] of presence?.others() ?? []) {
console.log(sessionId, entry.data.name, entry.data.cursor);
}
// React to changes.
const unsubscribe = presence?.subscribe({
onPresenceChange: () => render(presence.others()),
});
// Stop sharing.
presence?.clear();| Method | Returns |
|---|---|
selfId() | This session's presence id, once the session is open |
self() | This participant's own presence |
others() | A map from presence id to entry, without this participant |
all() | The same map including this participant |
set(data) | Publishes a new value |
clear() | Removes this participant's presence |
subscribe() | Registers an onPresenceChange listener; returns an unsubscribe function |
Each entry is { data, userId? }. data is the participant's presence,
decoded with your presence schema; a value that does not match it is not
shown. userId is the subject (sub) of the document token the participant
connected with, when its token named one, as the server verified it. To show
who someone is, put their name or your own user id in the presence data, or
mint each user's token with their id as its subject.
set takes effect locally at once: self() returns the new value without
waiting for the server.
After a reconnect the client publishes its last value again, under a new presence id. Treat presence ids as temporary: key your UI on something in the presence data if it must survive a reconnect.
Presence of backend participants
A backend without a session can appear among a document's participants with a presence lease: a value it sets over the HTTP API and renews before its lifetime ends. Sessions see it like any other participant's presence, with the subject of the token the backend set it with. See Transactions from a backend.
Next
- Zustand bindings: presence in a store, updated without re-rendering document selectors.