Skip to content

Real-time

Introduction

Go from "asking every few seconds whether there is a new message" to "the server tells you directly when there is one".

Why use it

Chat, order-status updates, collaborative editing, live dashboards, and AI token-by-token output all need the server to “push”. HTTP is request-response by nature, so you need one of these techniques.

How to do it

Technique Direction Traits Best for
Short polling Client asks repeatedly Simplest, most wasteful Low frequency, delay-tolerant
Long polling Client asks; server answers only when there is a message Best compatibility Fallback when nothing else works
SSE Server pushes one way Plain HTTP; browsers reconnect automatically Notifications, progress, AI streaming
WebSocket Bidirectional Low latency, full-featured Chat, games, collaborative editing

Rule of thumb: server push only → prefer SSE first; high-frequency bidirectional interaction → WebSocket.

Component comparison

SignalR

Built into the frameworkOpsLow

A .NET real-time communication library that lets the server push messages to browsers proactively, and automatically falls back and retries when a connection method fails.

Best forChat, notifications and live dashboards in .NET projects.

Avoid whenThe backend is not .NET, or you need interoperability with third parties over standard WebSocket.

Pros

It can automatically pick an available transport, such as WebSocket

Groups, broadcast, and per-user push are all built in

Reconnection after a disconnect has ready-made support

Cons

It fits .NET only, so Python projects cannot use it

Multiple servers need a separate Redis to sync messages

The protocol is its own, so ordinary clients cannot connect

Official docs ↗

Native WebSocket

Self-hosted open sourceOpen sourceOpsMedium

The browser and server establish a long-lived two-way channel, and either side can send messages at any time.

Best forFeatures that need two-way, high-frequency real-time interaction.

Avoid whenThe server only occasionally pushes a notification to the page; SSE is simpler.

Pros

Two-way real-time, suited to chat, collaborative editing and online games

A standard protocol supported by almost every language and browser

Low latency and small overhead once the connection is established

Cons

Reconnection and heartbeat detection must be written yourself

Connections hold server resources continuously, so capacity must be planned for many users

Some proxies and firewalls need special configuration to let it through

SSE (Server-Sent Events)

Self-hosted open sourceOpen sourceOpsLow

The browser keeps one connection open and the server can keep pushing messages into it, but only from server to browser.

Best forScenarios where "the server only needs to push out", such as notifications, task progress and streaming AI replies.

Avoid whenYou need two-way high-frequency interaction (such as chat rooms or collaborative editing).

Pros

It uses ordinary HTTP, so proxies and firewalls usually allow it

The browser reconnects automatically after a disconnect

Simple to build, and a good fit for notices, progress, and character-by-character output

Cons

The server can only push downward; sending upward still needs a normal request

Proxies buffer output by default, and that must be turned off for live updates

Under HTTP/1.1 the number of connections per domain is limited

Long polling

Hand-rolledOpen sourceOpsMedium

After the browser sends a request, the server holds off replying until there is a new message, and the browser immediately sends the next request once it receives one.

Best forScenarios with few users, low real-time requirements, or network environments that restrict other methods.

Avoid whenLarge user counts, or when you need genuinely low-latency real-time interaction.

Pros

Uses only ordinary HTTP requests, so compatibility is the best and it runs in any environment

Simple to reason about, needing no special protocol

A reliable fallback when WebSocket cannot be used

Cons

Every message requires a new request, so overhead is higher than WebSocket

The server must hold many waiting requests at once, which strains it with many users

Message timeliness and ordering must be handled yourself, making missed or duplicate messages likely

Socket.IO

Open-source libraryOpen sourceOpsLow

The most widely used real-time communication library on Node.js, adding rooms, broadcasting, automatic reconnection, and fallback transports on top of WebSocket.

Best forNode projects such as chat, collaboration, and real-time notifications that need rooms and broadcasting.

Avoid whenYou only need one-way push (SSE is simpler), or you need standard WebSocket interoperability with third-party clients.

Pros

Rooms, namespaces, broadcasting, and more work out of the box

Built-in automatic reconnection and acknowledgements

Integrates with NestJS (@nestjs/websockets), Express, and Fastify

Cons

It has its own protocol, so clients must use the Socket.IO client library

Multi-instance deployment needs an adapter such as Redis for broadcasting

Connection authentication and per-message permission checks are yours to implement

Official docs ↗

Axum WebSocket

Open-source libraryOpen sourceOpsLow

Use Axum's WebSocket upgrade with tokio-tungstenite for a bidirectional real-time channel.

Best forRust backends that need standard WebSocket.

Avoid whenYou only need one-way push (use SSE).

Pros

Fits Axum routes; types and middleware reuse

Standard WebSocket, no proprietary client protocol

Authenticate on the connection before upgrading

Cons

Rooms, broadcast, and reconnection are hand-built or need another library

Multi-instance broadcast needs Redis or similar

SSE is simpler for one-way push

Official docs ↗

Go WebSocket

Open-source libraryOpen sourceOpsLow

Use gorilla/websocket or coder/websocket for a bidirectional real-time channel.

Best forGo backends that need standard WebSocket.

Avoid whenYou only need one-way push (use SSE).

Pros

Mature ecosystem and plenty of material

Standard WebSocket, easy for any client

Works with net/http, chi, and Gin

Cons

Rooms and broadcast are hand-built

Multi-instance needs external pub/sub

Auth and heartbeats are yours to write

Official docs ↗

Checklist

  • Confirm you really need “push”; timed refresh is fine for simple cases
  • Prefer SSE when one-way push is enough; do not jump to WebSocket by default
  • Authenticate identity and permissions when the connection is established
  • Clients reconnect automatically; messages can be duplicated or backfilled (for example with a last-seen message ID)
  • Multi-instance setups have a cross-machine broadcast plan
  • The proxy layer allows long-lived connections; load-test concurrent connection counts