# Presence

> Show who is in the room and what each peer is doing, with heartbeats and a snapshot for late joiners.

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

1. **Announce on change.** When your own presence changes, broadcast it.
2. **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.
3. **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](https://jsflow.com/docs/guides/keeping-state-in-sync.md#2-give-the-state-to-whoever-arrives).
4. **Forget on departure.** On a `peer` event with `up` false, remove that peer from the picture.
5. **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.

```ts check title="app.ts"
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

- **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 a `visibilitychange` handler. See [lifecycle](https://jsflow.com/docs/concepts/lifecycle.md).
- **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](https://jsflow.com/docs/guides/security-and-privacy.md).

## An example you can run

The [`presence` example](https://github.com/precisit/jsflow-community/tree/main/examples/presence) (in the `examples/` folder of the [jsflow-community repository](https://github.com/precisit/jsflow-community)) is a working page: sections to move between, a heartbeat, a snapshot for late joiners, idle detection and one colour per peer.

Source: https://jsflow.com/docs/guides/presence/
