The socket reconnected; the messages did not
A hypothetical chat client reconnects successfully after an elevator ride. Its socket is open and heartbeat is healthy, but three messages are missing.
A live connection proves reachability now. It says nothing about the interval when there was none.
Choose the conversation's direction
Long polling holds an HTTP request until data or timeout, then the client issues another. It can use asynchronous I/O rather than one thread per waiter, but connections and buffers still cost resources. SSE sends a server-to-client text stream over HTTP. WebSocket supplies bidirectional framed messages over a persistent connection.
A dashboard only receives updates. Does choosing WebSocket automatically give it better delivery or recovery than SSE?
With SSE, an event can carry an id; browser reconnection can send Last-Event-ID. The server must actually retain and replay the missing events. If the cursor is older than retention, return a resynchronization path instead of pretending nothing was missed. Proxy buffering and idle timeouts must be configured/tested for streaming.
Turn a socket into a recoverable session
pseudocodeconnect with authentication and last applied cursor authorize channel/subscription if cursor outside retention: fetch snapshot and consistent stream position replay retained events after cursor join live delivery without a gap client deduplicates by event ID and advances durable cursor
This is a protocol sketch. The replay/live handoff needs a stable position so an event cannot fall between āhistory finishedā and āsubscription started.ā Order is scoped to a conversation or partition; merging channels requires defined semantics.
A gateway owns its local sockets. A registry can map user/session IDs to gateway/connection generations, while a backplane routes live notifications. A transient Pub/Sub backplane alone does not supply offline history; persist messages separately. A stale disconnect must not delete a newer connection record, so condition cleanup on connection identity/generation.
Reconnection is a load event
A gateway restart can trigger thousands of TLS/auth/subscription operations at once. Use jittered reconnect, bounded server admission, graceful draining, and staggered deploys. Warm authentication and replay dependencies too; accepting sockets faster than history can replay merely moves the queue.
Heartbeats detect idle or half-open connections under an explicit timeout policy. They do not prove application processing. Slow consumers need bounded send buffers and a policy: disconnect/resume, coalesce replaceable state, or drop explicitly lossy updates. Never silently discard ordered durable messages while claiming delivery.
Budget file descriptors, connection memory, TLS CPU, outbound bandwidth, heartbeat work, and replay throughput. Raising a kernel limit is not evidence that a process can sustain a million useful sessions. Reauthorize subscriptions as needed; long-lived connections must account for credential expiry and access revocation.
Mental model: the transport carries the present. A durable cursor, replay source, and resynchronization protocol recover the past.
Transfer: the price ticker
A price display may replace intermediate ticks with the latest quote. A trade ledger may not discard executions. Can both use the same slow-consumer policy?
Source trail: 18-websocket-sse-and-long-polling. See the source map for consolidation and reference-retained material.