WebSockets, SSE, Reconnection Jitter & Consumer Backpressure
Use SSE for server-to-client feeds and WebSocketStream when duplex frames need Streams API backpressure. Pair reconnects with full-jitter backoff and AbortController to prevent retry storms.
Stream Backpressure & Full-Jitter Reconnect
Trace how consumer backpressure throttles socket reads and how full-jitter backoff spreads reconnects.
UI renderer pauses reader.read() during a heavy frame, filling the ReadableStream highWaterMark.
if (controller.desiredSize <= 0) socketReader.pause();- desiredSize
- 0
- tcpWindow
- shrinking
- heapMb
- 42
Under the Hood
Legacy WebSocket.onmessage pushes every arriving TCP frame onto the main-thread event loop regardless of whether the UI can render it, growing heap memory without limit. Wrapping transport in a ReadableStream connects consumer desiredSize directly to TCP flow control.
- Transport Split: SSE (
text/event-stream) rides standard HTTP/2 multiplexing with nativeLast-Event-IDresume, whereas WebSockets upgrade to a dedicated full-duplex TCP framing layer. - Backpressure Propagation: When a consumer pauses
reader.read(),desiredSizehits zero andWebSocketStreamstops reading from the OS socket buffer, shrinking the TCP receive window. - Full-Jitter Backoff: Computing
random(0, min(cap, base * 2^attempt))spreads 100,000 disconnected clients uniformly across the retry window instead of spiking at fixed powers of two.
async function connectWithJitter(url, signal, attempt = 0) {
try {
const res = await fetch(url, { signal, headers: { Accept: 'text/event-stream' } });
return res.body.pipeThrough(new TextDecoderStream());
} catch (err) {
if (signal.aborted) throw err;
const delay = Math.random() * Math.min(30000, 500 * 2 ** attempt);
await new Promise((r) => setTimeout(r, delay));
return connectWithJitter(url, signal, attempt + 1);
}
}Problem Genealogy
- 2011 (Raw
WebSocket& FixedsetInterval): Clients buffered unboundedonmessageevents in JS arrays and retried every 1000ms, crashing tabs on bursts and taking down edge proxies after deploys. - 2015 (HTTP/2 + SSE Adoption): Unidirectional feeds moved to
EventSource, eliminating custom heartbeat frames and sharing one TCP connection per origin. - 2024 (
WebSocketStream&fetchStreams): Native WHATWG streams unified socket reads withReadableStreambackpressure andAbortSignalteardown.
Production Pitfalls
- Synchronized Retry Storms: Fixed or unjittered exponential backoff causes every dropped client to reconnect at the exact same second, turning a 5-second deploy blip into a multi-minute outage.
- EventSource 6-Connection Limit on HTTP/1.1: Opening multiple
EventSourcetabs against an HTTP/1.1 proxy exhausts the browser's 6-connection-per-origin pool and stalls APIfetchcalls.
- When not to use WebSockets: Do not deploy WebSockets for server-to-client notifications, LLM token streaming, or live tickers; use HTTP/2 SSE to avoid stateful load-balancer stickiness and custom auth-header workarounds.
Connections & Logical Next Steps
ReadableStream desiredSize signals allow slow consumers to exert TCP-level backpressure on WebSocketStream.
AbortController cancels pending reconnect timers and in-flight SSE fetch streams cleanly on unmount.