Skip to content

Lesson 3 of 9 · 21m

SSE and Polling: Fallbacks That Are Features, Not Failures

When Server-Sent Events or polling are the better transport, how fallback ordering works, and what proxies do to long-lived responses.

WebSocket is the best general-purpose realtime transport, but it is not the only correct answer. Server-Sent Events are simpler for one-way server-to-client streams, and polling is still the most debuggable system ever invented. Senior engineers choose the transport after looking at direction, infrastructure, and failure behavior.

SSE is HTTP with a long-lived response

HTTP/1.1 200 OK
Content-Type: text/event-stream
Cache-Control: no-cache
Connection: keep-alive

id: 1842
event: message
data: {"room":"room:42","body":"hello"}

: heartbeat
const source = new EventSource("/events");
source.addEventListener("message", (e) => console.log(e.lastEventId, JSON.parse(e.data)));
source.onerror = () => console.log("disconnected; browser retries with Last-Event-ID");

SSE gives you automatic reconnect, resume via Last-Event-ID, HTTP middleware compatibility, and trivial curl debugging. It does not give you client-to-server realtime messages or binary frames. For dashboards, notifications, feeds, and progress streams, that trade is often excellent.

The proxy decides whether streaming works

Buffering is the classic SSE failure. The app emits events, nginx buffers the response, and users see nothing until the buffer fills. Disable buffering and transformation on stream routes:

location /events {
    proxy_pass http://app;
    proxy_http_version 1.1;
    proxy_set_header Connection "";
    proxy_buffering off;
    proxy_read_timeout 3600s;
    add_header X-Accel-Buffering no;
}

Polling is a valid transport

async function poll(cursor) {
  const page = await (await fetch(`/api/feed?after=${cursor}`, { cache: "no-store" })).json();
  render(page.items); setTimeout(() => poll(page.cursor), page.items.length ? 2000 : 8000);
}

Polling wins when updates are low-frequency, strict realtime has no user value, corporate networks break streaming, or you need simple horizontal scaling. Add jitter to intervals or clients synchronize into thundering herds. Use conditional requests (ETag, 304) or cursors so empty polls are cheap.

Fallback is a product decision

Do not silently downgrade a collaborative editor from WebSocket to five-second polling and pretend nothing changed. Latency affects correctness and user trust. Expose transport state in the UI when it matters, and design protocols so snapshots plus deltas work over any transport.

Field notes

  • SSE over HTTP/1.1 is limited by browser per-host connection counts; HTTP/2 removes most of that pain.
  • EventSource cannot send custom headers either. Use cookies or minted query tokens.
  • Never let a service worker cache an event-stream response.
  • Load balancers often have default 60 second idle timeouts. Either heartbeat faster than that or configure the route explicitly.

Was this lesson helpful?

Sign in to rate