Server-Sent Events and Polling

Choose the simplest real-time technique that works: short polling, long polling or one-way SSE streams.

Intermediate · about 3 hours · difficulty 2/5

Learn first: HTTP in Depth

Not every live feature needs WebSockets. If data only flows from server to client (notifications, progress bars, live scores, streaming AI responses), simpler options are easier to build, cache and scale.

Short polling

The client asks every N seconds: GET /jobs/42/status. Trivial to implement and works everywhere, but wastes requests when nothing changes and adds up to N seconds of delay. Perfectly fine for dashboards refreshing every 30 seconds. Use ETags so unchanged responses are cheap.

Long polling

The client makes a request and the server holds it open until there is new data or a timeout (say 30 seconds), then the client immediately asks again. Near real-time with plain HTTP, but each waiting client ties up a connection, and you must handle timeouts carefully.

Server-Sent Events

SSE keeps one HTTP response open and streams events in a simple text format (event:, data:, id: lines). The browser's EventSource API handles it natively and reconnects automatically, sending Last-Event-ID so the server can resume. It works through most proxies and over HTTP/2, where many streams share one connection. Limits: one-way only (client-to-server still uses normal requests), text only, and over HTTP/1.1 browsers cap connections per domain.

Choosing

  • Occasional updates, low urgency: short polling.
  • One-way live updates, streaming responses: SSE.
  • Frequent two-way messages, low latency: WebSockets.

Webhooks cover the server-to-server version of the same problem.

Pitfalls

Proxies that buffer responses break SSE; disable buffering (for example X-Accel-Buffering: no in Nginx) and send periodic comment lines as keep-alives.

You are ready to move on when you can stream job progress to a browser over SSE with automatic resume after a disconnect, and justify SSE vs WebSockets for a given feature.

Key concepts

  • Short polling
  • Long polling
  • Server-Sent Events format
  • EventSource and automatic reconnect
  • Last-Event-ID resume
  • Proxy buffering pitfalls
  • Choosing SSE vs WebSockets

Mini project

Build a report-export feature that streams progress percentages over SSE, resumes correctly after a network drop, and falls back to polling a status endpoint.

Check yourself

Why is SSE a better fit than WebSockets for streaming an AI model's response token by token?

Free resources