A connected socket does not prove a person is actively reading a page, and a disconnected socket may not be noticed immediately when a device sleeps. Presence is an estimate with an expiry time, not a durable business fact. Heartbeats help detect dead connections, but they consume bandwidth and battery and should respect infrastructure idle behavior. Deployments add another boundary: a server instance may need to stop accepting new sockets, tell existing clients to reconnect, and release its resources before shutdown. Durable case data must survive this drain; typing and viewing hints may expire naturally.
Presence Expiry, Heartbeats, and Connection Drain
Working case
Reviewer 47 opens case 29 on a shared laptop, then the lid closes. The server still shows them as present for several minutes because the connection has not timed out. Another reviewer delays an edit, thinking reviewer 47 is working. The interface changes its label to “recently active” after the presence lease expires and never treats presence as an edit lock. During a release, the old socket node stops accepting new subscriptions, sends a reconnect hint where possible, and closes connections after a bounded drain. Clients reconnect with fresh authorization and reload missing case versions.
Implementation boundary
function presenceLabel(lastSeenSecondsAgo, leaseSeconds) {
return lastSeenSecondsAgo < leaseSeconds ? "recently-active" : "unknown";
}
console.log(presenceLabel(63, 47));
// Output: unknownDefine presence keys by actor, case, and session with short expiry; refresh them only on meaningful activity or a bounded heartbeat. Remove a lease on clean disconnect but rely on expiry for crashes, sleeping devices, and lost close frames. Display time-aware wording and avoid claiming certainty about a person’s attention. Make the server heartbeat interval and timeout compatible with proxy idle limits and device cost; do not send rapid pings merely to keep a vanity online badge. On deployment, mark the instance draining, stop new upgrades, and give existing sockets a deadline to reconnect. Persist durable events before fanout so drain cannot erase committed changes. A client reconnect uses backoff with jitter, fresh session validation, and gap reconciliation.
Cost and boundaries
Heartbeat traffic is O(C / I) messages per unit time for C connections and interval I, plus timer and connection memory. Shorter presence expiry reduces false online time but increases flicker and refresh work. Shared presence storage adds write load; avoid one write per pointer movement. Draining an instance temporarily increases reconnection traffic and can create a thundering herd if clients retry at the same interval. Measure false-presence duration, heartbeat bytes, idle close rate, reconnection spread, drain completion time, and event gaps after deployments. Treat presence history as private telemetry with limited retention.
Failure trace
Sleep a laptop without sending a close frame and confirm presence expires. Drop one heartbeat response and avoid immediately marking the user offline if the timeout contract allows recovery. Deploy a new socket node while 900 clients are connected; the old node must stop new upgrades and close within its drain deadline. Have all clients reconnect simultaneously without jitter and observe the surge, then apply bounded randomized backoff. Revoke reviewer 47’s session during drain and ensure the new connection cannot rejoin case 29. Publish a durable event at shutdown and verify it is available through replay.
Verification
- Presence expires when a device disappears without a close frame.
- Rolling drain preserves durable event replay.
- Reconnecting clients use backoff and current authorization.
Practice drill
Create 63 presence leases for case 29 with a 47-second expiry. Let one device sleep, one tab close cleanly, and one socket remain active through a 90-second rolling deployment. Record the visible state after each expiry, reconnect attempt times, and the final case event version. Simulate a proxy idle timeout shorter than the planned heartbeat interval and adjust the policy with measured traffic. Confirm that a presence lease never grants edit permission or prevents a valid save.
Decision note
Presence is expiring advisory state; durable case versions and current permission survive every connection drain.
Common Mistakes
- Using a connected socket as proof of active human attention.
- Keeping presence forever after a missed disconnect.
- Restarting nodes without a drain and replay plan.
Related lessons
Realtime Connection and Event Delivery; WebSocket Handshake, Session, and Channel Authorization; Event Sequence, Replay, and Gap Reconciliation; Socket Fanout, Backpressure, and Slow Consumers; Collaborative Conflicts and Presence Expiry; Live Update Reconnect and Event Ordering.
Apply and check
Build Project: live permit case board and review Web Development: realtime delivery contracts quiz.
