Polling, SSE and WebSockets

API design: REST, HTTP, GraphQL and gRPC Mid-level Python 3.13+ PostgreSQL 17+ Reviewed By Best Latest Apps

Pick the fix

A stock ticker page needs server-to-browser price updates once a second; no client messages. Best fit?

Options
Hint 1 of 1

Which way does data flow?

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Server-sent events

Correct. One-way stream over HTTP with built-in reconnect and resume.

Server-sent events are a plain HTTP response that stays open and streams events one way. The browser's EventSource reconnects on its own and sends Last-Event-ID to resume, which is all a ticker needs.

Why not the others?
  • B WebSockets. Works, but adds a two-way protocol you don't need.
  • C Polling every 100 ms. Ten times the requests for one update a second.
  • D Webhooks. Webhooks call servers, not browsers.

30-second answer

Polling is simplest and works everywhere; fine when updates every few seconds are enough. Server-sent events keep one HTTP response open and stream text events from server to client, with automatic reconnect and Last-Event-ID resume, which suits one-way feeds. WebSockets give a full two-way channel, for chat or collaborative editing, but need their own auth, heartbeats and load-balancer support.

Practise saying it · 30 s. No recording, just a timer

Pick a real-time transport.

Say your answer out loud, then check which of these points you covered:

  • Polling: simple
  • SSE: one-way, resume
  • WebSockets: two-way
  • Pub/sub for fan-out
  • Push for background mobile

Deep dive

Long-lived connections change capacity planning: count open connections, not requests per second.

Behind load balancers, set idle timeouts above your heartbeat interval.

Fan-out across instances needs a pub/sub layer (Redis, NATS, Kafka) so any instance can push to any client.

Mobile apps in the background can't hold sockets; use push notifications.

Common mistakes

  • WebSockets for a one-way feed.
  • Polling every 100 ms from thousands of clients.
  • No resume, so clients miss events during reconnects.

Senior take & follow-ups

Match each option to a need, then the operational costs of long-lived connections.

Follow-up questions

Think of your answer first, then open the hint.

  1. How does SSE resume?

    Show hint

    The browser sends Last-Event-ID; the server replays from there.

  2. How do you scale WebSockets?

    Show hint

    Sticky or connection-aware routing plus pub/sub between instances.

Suggest a fix if something here is wrong or out of date.