In a team wiki's collaborative page editor, how would you show each collaborator's cursor and selection so they stay on the right characters while others type?
answer
- ephemeral, never in the document
- anchor to characters, not pixels
- re-resolve after every remote edit
- throttle sends, draw once per frame
- name, colour, fade when idle
basics
~20 sTreat cursors as ephemeral presence, not edits: send selections as anchors tied to characters (transformed offsets in OT, character identities in a CRDT), re-resolve them after every edit, throttle sends, and draw named carets that fade when idle.
solid answer
~50 sPresence is ephemeral and stays out of the document: no history, no undo, no snapshot. Each page sends its selection as an anchor and a head tied to characters, not pixels or raw offsets — in an OT editor an offset transformed by every incoming operation, in a CRDT the identity of the character it sits beside. The receiver re-resolves each remote anchor to a screen position after every edit and layout change, so a caret stays on its word while text above it grows. Sends happen only on change, a few per second, with the latest replacing older ones; drawing happens once per animation frame on an overlay layer. Each caret carries the person's name and colour, the label fades after a few idle seconds, a cursor scrolled out of view becomes an edge marker, and sends pause while the tab is hidden.
code
json · 10 lines{
"type": "presence",
"user": { "id": "u-481", "name": "Ana", "color": "#8a2be2" },
"selection": {
"anchor": { "afterChar": "site7:1042" },
"head": { "afterChar": "site7:1063" }
},
"state": "active",
"seq": 311
}go deeper
Recall that remote cursors are temporary presence data, kept separate from the document, its history and its undo.
Explain why a raw offset drifts when text above changes, and how transforming offsets or anchoring to character identities keeps a caret on its word.
Show the send policy — on change, throttled, latest wins, paused when hidden — and a drawing pipeline that copes with layout changes and collaborators out of view.
Decide how much presence is worth at thirty editors: fan-out cost, screen noise and privacy, and set rate, label and announcement limits as product policy.
## Presence is not the document In a **team wiki's page editor**, each collaborator's caret and selection is **presence**: state that only matters while people are looking at the page. It changes many times a second, nobody wants it in the page history, undo must never move it, and losing it on a disconnect costs nothing because the next update replaces it. So it travels as its own message type beside the document's operations and is never written into the shared document, its snapshots or its operation log. How the server stores and fans out presence, and how the connection reconnects, belong to the server-side presence question and the real-time transport question; this answer covers what the page sends and draws. ## Anchoring so cursors stay put The hard part is position. Ana's caret sits on a word; Ben inserts a sentence above it. Her caret must move with the word. | What the page sends | What happens when text above it changes | |---|---| | pixel coordinates | wrong on any screen with a different width, font or zoom | | raw character offset | points at the wrong character after any insert or delete before it | | offset transformed by each later operation (OT) | stays correct, as long as every operation is applied to it | | identity of the adjacent character (CRDT) | stays correct; resolved to an offset only when drawing | A selection is two such anchors: where it started and where the caret is. With OT, a remote cursor is pushed through the same transformation as text, like an insert of zero length. With a sequence CRDT, the anchor names the character it sits after (or before), so it resolves correctly however the text around it changed. A presence message can arrive before the edit it refers to; if an anchor names a character this page has not received yet, hold the message until it resolves or drop it — a newer update comes soon anyway. ## Sending: throttle and replace - Send **only on change**, throttled to a few updates per second per person; a burst of keystrokes yields one or two presence messages, not one per key. - Keep **latest wins per person**: an older position that arrives late is discarded, never queued. An increasing sequence number per person tells newer from older without trusting anyone's clock. - **Pause** sending while the tab is hidden and mark the person idle; animation-frame callbacks are paused in most browsers in background tabs anyway, so a hidden page should not be producing caret traffic. - Remove a collaborator's caret when they leave or when the server reports them gone. With thirty editors, unthrottled caret updates would multiply into hundreds of messages a second, each fanned out to every other open page. ## Drawing 1. Keep remote carets and selection highlights on an **overlay layer** above the editable content, never as markup inside it, so they cannot leak into what the user copies or into the document. 2. On an animation frame where something changed, resolve every anchor to a text position and measure its rectangles from the laid-out text; a selection across lines becomes several rectangles. 3. Re-measure after remote edits, after resizing and after images load, because layout moves even when anchors do not. 4. Label each caret with the person's **name** and a stable, distinguishable **colour** — picked from a palette with enough contrast against both the text and the selection highlight, and kept for the whole session so others learn it; fade the label after a few seconds without movement and keep a thin caret. 5. When a collaborator's caret is above or below the viewport, show an edge marker ("Ana is editing below") that scrolls to them when clicked. ## Accessibility and restraint The decorations are visual, so hide them from assistive technology and offer the same information as text — a "who is here" list naming each person and the heading they are in. Do not announce every caret movement through a live region: thirty people typing would drown a screen-reader user's own work. Announce joins and leaves at most, and only when the user opts in. Presence also exposes what people are doing, so show it only to people who can open the page, and never record it.
- A presence message names a character the page has not received yet. What do you do?Hold it briefly until the edit that created that character arrives, then resolve it; if a newer presence message from the same person comes first, drop the older one. Never guess a position — a caret drawn in the wrong paragraph misleads more than one that appears a moment late.
- The same person has the page open in two tabs. How should presence show that?Group presence by person, not by connection: show one name in the 'who is here' list and draw the selection from the tab they used most recently. Two carets labelled with one name look like a bug and double the traffic.
A remote cursor is like a sticky tab on a word in a shared printed draft: when someone pastes a paragraph above, the tab moves with its word, while a pencil mark measured from the top of the page would now point at the wrong line.
- Store each collaborator's cursor in the shared document so everyone sees it.
- Send cursor pixel coordinates and draw them at the same spot on every screen.
- A plain character offset stays correct after others insert text above it.
- Broadcast every caret movement immediately, because presence must be real-time.
- Announce each remote cursor move to screen-reader users through a live region.