Entrega en Tiempo Real

Polling · SSE · WebSockets

Escrito por Darjus Kazakevic · revisado el

Use the simplest push that is fast enough, then plan for open connections.

HTTP was built for the client to ask and the server to answer. Chat, live scores, notifications and dashboards need the server to send data as soon as it exists. Four techniques do this: short polling, long polling, Server-Sent Events (SSE) and WebSockets. Each one has lower latency and keeps more connections open.

The hard part is scale, not the protocol. One server can hold tens of thousands of open connections, but each user is connected to only one server. A message for that user must find the right server, usually through a pub/sub backplane such as Redis or NATS.

En esencia — Choose the simplest push method that meets your latency target, and route messages between servers through a pub/sub backplane.

Cuándo recurrir a él

Recurre a él cuando

  • Users must see new data within a second or two: chat, alerts, prices
  • The server has updates more often than clients would poll
  • Many users watch the same screen and must see the same changes

Reconsidéralo cuando

  • Data changes every few minutes: poll, or send a push notification
  • The client needs one answer to one request: use plain HTTP
  • Delivery must reach users who are offline: store it, then notify

Conceptos clave

Short polling
The client asks every few seconds whether anything is new. It is plain HTTP, easy to cache and easy to scale. Most answers are empty, and an update waits up to one full interval.
Long polling
The client asks, and the server holds the request open until there is news or about 30 seconds pass. The client then asks again at once. Latency is low, and it works through any proxy.
Server-Sent Events (SSE · EventSource)
One long HTTP response with Content-Type: text/event-stream. The server writes events as lines. The browser's EventSource reconnects by itself and sends Last-Event-ID, so the server can resume.
WebSockets
An HTTP request upgraded to a two-way channel. Both sides send small frames at any time, with only a few bytes of overhead. Use it when the client also sends often: chat, games, shared editing.
Backplane and fan-out
Servers publish outgoing messages to a shared pub/sub channel, and each server forwards them to the connections it holds. Redis pub/sub, NATS or Kafka can play this role.

Reglas de diseño

Start with the simplest method that is fast enough
Polling every 10–30 s is fine for many dashboards. Use SSE when only the server pushes. Pick WebSockets only when the client also sends often, because you then own reconnects.
Plan for reconnects and missed messages
Connections drop on every deploy and network change. Give each event an ID, let the client resume from the last one it saw, and add random jitter to reconnects.
Route messages through a backplane
Each user is connected to one server out of many. Publish each message to a channel such as user:42 in Redis or NATS, and let the server holding that connection deliver it.
Size servers by open connections
Each connection costs memory and a file descriptor. Plan for roughly 10k–50k per instance, raise OS limits (ulimit -n), and send a heartbeat every 30 s so proxies keep idle connections.

Errores habituales

  • Sending only to local connections. It works with one server; with two, about half the messages never arrive. Add a backplane before the second server.
  • Every client reconnects in the same second after a deploy, and the new servers fall over. Add random jitter and drain old servers slowly.
  • A proxy or load balancer closes idle connections after 60 seconds, so clients reconnect again and again. Send a heartbeat every 30 seconds.
  • Treating the connection as the only delivery path. A user offline for 10 seconds loses messages. Store them, and let the client fetch what it missed by ID.