Presence is the small piece of state each peer owns about itself: a name, a status, the section it is looking at. Every peer holds a picture of all the others’.
The pattern
Section titled “The pattern”- Announce on change. When your own presence changes, broadcast it.
- Heartbeat. Also broadcast it every few seconds (4 to 5 seconds is typical). This repairs lost broadcasts, and tells the others you are still there.
- Snapshot for arrivals. When a peer becomes reachable, send it your presence with
sendReliable, so it sees you at once instead of waiting for the next heartbeat. Presence is set-like (each peer sends its own entry, and the order does not matter), so every peer sending is right here; an ordered history needs one source. - Forget on departure. On a
peerevent withupfalse, remove that peer from the picture. - Mark the quiet as idle. Record when you last heard from each peer, and show a peer you have not heard from for a few heartbeats (about three) as idle rather than removing it: a phone that is asleep stops sending but may be back in a moment.
import { join, text } from 'jsflow-sdk';
const room = await join({ app: 'pk_…', room: 'team-7' });const me = { name: 'Guest', section: 'Overview' };type Seen = { name: string; section: string; seen: number };const picture = new Map<number, Seen>();const hello = () => JSON.stringify({ t: 'presence', ...me });const announce = () => room.broadcast(hello());
room.on('message', (from, data) => { const m = JSON.parse(text(data)); if (m.t === 'presence') { const { name, section } = m; picture.set(from, { name, section, seen: Date.now() }); }});room.on('peer', (id, up) => { if (up) room.sendReliable(id, hello()); else picture.delete(id);});setInterval(announce, 4000);
export const isIdle = (id: number) => Date.now() - (picture.get(id)?.seen ?? 0) > 12_000;Details worth getting right
Section titled “Details worth getting right”- Ids are per join. A reload is a new peer with a new id. If you want a person to keep their name and colour, derive them from your own user id (see join tickets), not from the peer id.
- One colour per peer. If you show a colour for each peer, derive it from the id alone (for example a hue from the id multiplied by the golden angle), so it is the same on every screen without anyone agreeing on it.
- Phones and Safari. A hidden page says goodbye (the room state is
away) and comes back with the same id. If your app should show “away” rather than “gone”, send that status yourself from avisibilitychangehandler. See lifecycle. - Keep it small. Presence is sent often: a few dozen bytes. Do not put the document in it.
- Do not trust it. Presence arrives by broadcast: signed, so it really comes from that peer, but not sealed, and a peer can say anything about itself. Use it for display, not for permissions. See security and privacy.
An example you can run
Section titled “An example you can run”The presence example (in the examples/ folder of the jsflow-community repository) is a working page: sections to move between, a heartbeat, a snapshot for late joiners, idle detection and one colour per peer.