0203. Presence interest management: per-recipient frames, viewport filtering, follow, idle suppression
Status: Accepted (2026-11-28). Wave 3, R1. Refines the presence rules of 0007 and the presence fan-out of 0201. Contract: follow in shared/src/presence.ts, PRESENCE_VIEW_MARGIN, MAX_VISIBLE_CURSORS, VIEWPORT_RELAY_MS in shared/src/limits.ts (PROTOCOL_VERSION 3).
Context
The 100k run (WP-H, 2026-11-27) found presence fan-out to be 85-90% of the frames the server writes, at about 7 us per frame (half of it kernel time):
- every room member got the room’s batch at every tick, including a sole editor’s own cursor (about 20% of presence sends);
- receivers were expected to filter by their own viewport, after the bytes and syscalls were spent;
- a cursor resent at the same position was relayed again.
Decision
The owner still merges presence per room tick. The gateway builds one frame per recipient (presence.ts buildFrame, called from Gateway.onPresenceRoom).
- Owner side. For each room tick the owner writes one internal record per gateway that has live members (
O2G.PRESENCE_ROOM).
- The record holds the changed entries. Each entry carries its fields, the cursor to test interest against, and the last relayed cursor (for exit updates).
- It also carries refresh groups for that gateway’s members.
- Large rooms (> 200) still tick at 5 Hz, and at most 50 changed peers are relayed per tick.
- Rules per recipient (one PRESENCE_BATCH per room per tick at most; binary layout unchanged):
- Own entry: never sent.
- Cursor, cursor hidden, drag: sent only when the recipient follows the peer, or has not sent a viewport yet, or when the peer’s cursor is inside the recipient’s viewport grown by
PRESENCE_VIEW_MARGIN (0.2 of width and height on each side). A peer whose previous relayed cursor was inside also gets through once more, so the viewer gets one exit update. At most MAX_VISIBLE_CURSORS (20) such entries per frame; the rest wait for later ticks.
- Selection, tool, drag end: to everyone in the room.
- Viewport: to followers every tick. Everyone else gets it at most every
VIEWPORT_RELAY_MS (1 s), trailing edge, so the last value always arrives.
- Refresh: when a recipient’s viewport changes, or it starts following someone, its next frame also carries the full state of peers whose cursor came into its view (in the new view, not in the old one), and of the new followee.
follow: a server-only field (flag 128, clientId varuint, 0 = none). It is merged and counted per followee, and never relayed.
- Idle suppression.
PresenceTable.mergeDiff reports which fields actually changed. An update that changes nothing is counted (linora_rt_presence_idle_total) and relayed nowhere.
- Writes.
- The frame is written once into a per-thread scratch buffer and sent as a cached view of its length, inside
ws.cork.
- uWS drains the socket’s pending topic messages (op broadcasts) into the same corked write first, so the socket gets one write per tick.
- Nothing is allocated per recipient: a unit test measures under 4 KB of new-space growth over 100,000 frames.
- Clients (sync-client, DC2): they hide cursors received outside their own viewport plus the margin, unless they follow that peer. Otherwise a cursor last seen at its exit point would come back stale after a pan.
Alternatives
- Topic publish with receiver-side filtering (before): one native publish per room per gateway, but every member pays the syscall and the bytes, and the sender gets its own echo.
- Per-recipient frames built on the owner: the owner has the state, but it is 2 threads against 5 gateways. Its ring traffic would also grow from one record per room per gateway to one per recipient.
- Per-(recipient, peer) “last sent” state for exact exit and enter handling: O(members²) per room. The previous-position test plus refresh-on-pan gives the same visual result with O(members) state.
- Spatial index of viewports per room: not worth it at <= 5,000 members per room. The cost is a few float compares per entry per recipient.
Consequences
- Presence frames out scale with the viewers who can actually see a cursor, not with room size. The realistic profile (ADR 0803) measures it.
- Followers see the followee wherever it is. A viewer without a viewport (agents, old clients) still gets everything.
- A recipient can miss a state-only entry if its gateway ring is full at that tick: presence records are shed, never queued (ADR 0204). The next change of that field corrects it.
- The realtime harness test
presence interest management covers no echo, viewport filtering, the exit update, refresh on pan, follow, the viewport throttle and idle suppression. r1-units covers frame building and the allocation-free fan-out.