Skip to content
Tutorials Center
My learning

Topics

Lesson 5 of 36 ยท Getting Started

Choosing a Real-Time Technology

Compare short polling, long polling, Server-Sent Events, raw WebSocket and Socket.IO. Run a real SSE example, learn the trade-offs and use a checklist to pick the right tool.

"Real-time" is a spectrum, and Socket.IO is one point on it. Before you reach for it, it pays to know the alternatives, because the simplest tool that meets the need is usually the best one. This lesson compares the five common techniques and gives you a way to choose.

The five options

Short polling. The client calls an HTTP endpoint on a timer. Easy, cacheable, works everywhere. Wasteful and laggy; the delay is up to the interval.

Long polling. The client calls, and the server holds the request open until it has something or a timeout hits; the client then immediately calls again. Lower delay than short polling, but each message costs a full HTTP round trip. This is what Socket.IO uses as its fallback transport.

Server-Sent Events (SSE). One long HTTP response with Content-Type: text/event-stream. The server writes lines like data: hello whenever it wants. Browsers expose it as EventSource with built-in automatic reconnection. One direction only (server to client); the client talks back with ordinary requests.

WebSocket. After an HTTP upgrade the connection becomes a persistent, two-way, message-based channel with very little overhead. You get raw messages and nothing else: no event names, no rooms, no reconnection, no fallback.

Socket.IO. Its own protocol over WebSocket (or long-polling), adding events, acknowledgements, rooms, namespaces, broadcasting, reconnection and middleware.

See SSE for real

SSE is simple enough to run for real in Node, because it is plain HTTP. The example below starts a local HTTP server that streams three events, and reads them with fetch. Nothing here is Socket.IO; it shows the "push" idea in its leanest form.

Node.js A real Server-Sent Events stream

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

In a browser you would write new EventSource('/stream') and source.addEventListener('tick', ...), and the browser would reconnect for you. For more, read Server-Sent Events in Node.js.

Comparison table

Short polling Long polling SSE WebSocket Socket.IO
Direction client pulls client pulls server to client both both
Latency up to interval low low lowest low
Overhead per message high high low very low low to medium
Works through strict proxies yes yes mostly sometimes not yes (fallback)
Auto-reconnect you write it you write it built in you write it built in
Rooms, broadcast, acks no no no no yes
Binary data yes yes no (text) yes yes

A few notes on the table:

  • SSE over HTTP/1.1 is limited by browsers to about six connections per host across all tabs; HTTP/2 removes that problem.
  • Raw WebSocket needs you to design a message format (for example { type, payload }), heartbeat logic and reconnection. Many teams end up rebuilding half of Socket.IO.
  • Socket.IO's protocol costs a few extra bytes per message and a heavier client download (around 15 kB gzipped for the client), which rarely matters but can for tiny widgets.

A decision checklist

Answer in order and stop at the first match:

  1. Is the data refreshed rarely or on user action? Use plain HTTP and a refresh or a slow poll.
  2. Does data only flow from the server to the client (notifications, tickers, logs, progress)? Use SSE.
  3. Do you need a standard WebSocket protocol, for example to talk to another vendor's service or a non-JavaScript client? Use WebSocket.
  4. Do you need two-way events with rooms, broadcasting, acknowledgements, automatic reconnection and a fallback? Use Socket.IO.

The same logic as code, with a few sample projects:

Node.js A tiny decision helper

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with node:readline or process.stdin), it gets the next line from this box.

Mixing technologies

You are not forced to choose one. A common design is: normal HTTP for loading and saving data (REST or GraphQL), plus Socket.IO only for the live part, such as "a new comment arrived" or "someone is typing". The events then act as hints: the server says what changed and the client may refetch the details over HTTP. That keeps your real data path simple and cacheable, and makes the live layer easy to replace later.

When designing live features, ask what happens when a message is lost or arrives twice. If the client can always refetch the truth after a reconnect, you can keep your events small and simple.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. A dashboard only needs to receive a continuous stream of metrics from the server. Which option is usually the simplest fit?

    Data flows one way, so SSE (plain HTTP with built-in browser reconnection) is the lightest choice.
  2. 2. Which is NOT something raw WebSocket gives you out of the box?

    WebSocket is only the transport. Reconnection, rooms and event names are things you build or get from a library.
  3. 3. How does long polling differ from short polling?

    Long polling leaves the request pending so data can be delivered immediately, then the client asks again.
  4. 4. Why might a team use Socket.IO only for 'hints' and fetch details over HTTP?

    Events say something changed; the authoritative data still comes from normal endpoints, so lost events are harmless.

Summary

  • Short polling, long polling, SSE, WebSocket and Socket.IO sit at different points between simplicity and features.
  • SSE is the lightest choice for server-to-client streams; raw WebSocket suits standard-protocol integrations.
  • Socket.IO adds events, acks, rooms, broadcasting, reconnection and a fallback on top of WebSocket.
  • Combining normal HTTP for data with Socket.IO for notifications is a robust pattern.
  • Pick the simplest tool that meets the requirement.

Next: The Connection Lifecycle