WebSocket vs Polling vs SSE — Choosing the Right Real-Time Strategy
Not everything needs WebSockets#
"We need real-time updates" doesn't automatically mean WebSockets. There are three main approaches, and the right one depends on your use case.
Short polling#
The simplest approach: the client asks the server for updates on a regular interval.
Client: Any new messages? (every 5 seconds)
Server: Nope.
Client: Any new messages?
Server: Nope.
Client: Any new messages?
Server: Yes, here's one.
Pros: Dead simple. Works everywhere. No special infrastructure. Cons: Wasteful — most requests return nothing. Latency equals your polling interval.
Use when: Dashboard that updates every 30 seconds. Background sync for non-urgent data. Simple health checks.
Long polling#
The client sends a request, and the server holds it open until there's new data (or a timeout).
Client: Any new messages? (hangs open)
... 30 seconds pass ...
Server: Yes, here's one.
Client: Any new messages? (immediately re-connects)
Pros: Near real-time without WebSocket complexity. Works through all firewalls and proxies. Cons: Still one connection per request cycle. Higher server resource usage from held connections. Ordering can be tricky.
Use when: Chat applications that need simplicity. Notifications where sub-second latency isn't critical. When WebSocket infrastructure isn't available.
Server-Sent Events (SSE)#
The server pushes events to the client over a single long-lived HTTP connection. One-directional: server → client only.
Client: Opens connection to /events
Server: data: {"type":"message","text":"Hello"}\n\n
Server: data: {"type":"message","text":"World"}\n\n
... connection stays open ...
Pros: Built into browsers (EventSource API). Auto-reconnect with last-event-ID. Works over standard HTTP. Simple server implementation. Cons: One-directional only. Limited to ~6 connections per domain in some browsers. No binary data.
Use when: Live feeds, stock tickers, notifications, build logs, AI streaming responses. Anywhere the server pushes and the client listens.
WebSocket#
Full-duplex, bidirectional communication over a single TCP connection. Both client and server can send messages at any time.
Client ↔ Server: persistent connection
Client: sends message
Server: sends message
(both directions, any time)
Pros: True bidirectional. Low latency. Low overhead per message (no HTTP headers). Binary data support. Cons: More complex infrastructure (sticky sessions, connection management). Doesn't work through some corporate proxies. Need heartbeat/ping to detect dead connections.
Use when: Chat applications, multiplayer games, collaborative editing, live trading platforms. Anywhere both sides need to send data frequently.
The decision framework#
| Requirement | Best choice |
|---|---|
| Updates every 30+ seconds | Short polling |
| Server pushes updates, client just listens | SSE |
| Bidirectional communication needed | WebSocket |
| Works through restrictive firewalls | Long polling or SSE |
| Binary data (audio, video signaling) | WebSocket |
| AI/LLM streaming responses | SSE |
| Multiplayer game state | WebSocket |
| Notification feed | SSE or long polling |
| Simple, no infrastructure changes | Short polling |
Scaling considerations#
Short polling: Scales easily — it's just regular HTTP requests. Use CDN caching for public data.
SSE: Each connected client holds an open HTTP connection. 10,000 users = 10,000 connections. Most web servers handle this fine with async I/O (Node.js, Go, Rust).
WebSocket: Same connection scaling as SSE, plus you need sticky sessions (the same client must hit the same server). Use Redis Pub/Sub to broadcast messages across multiple WebSocket servers.
Long polling: Hardest to scale. Each pending request consumes a server thread/connection. Similar to SSE but with more overhead from repeated connection setup.
The hybrid approach#
Most production apps use a mix:
- WebSocket for the core real-time feature (chat, collaboration)
- SSE for notifications and live updates
- Short polling for non-urgent background syncs
Codelit itself uses SSE for AI streaming (server pushes tokens as they're generated) — it's the perfect fit because the client just listens while the AI generates.
See real-time patterns in action: generate a chat or collaboration system on Codelit.io and explore the WebSocket connections in the architecture.
Explore the Discord architecture interactively
Try it →Go deeper on system design
Some links below are Amazon affiliate links. If you buy through them, Codelit earns a small commission at no extra cost to you. Recommendations are chosen on merit, not commission.
Building Microservices
Sam Newman · 2021
600 pages on splitting a monolith: service boundaries, data decomposition, migration order.
Enterprise Integration Patterns
Gregor Hohpe, Bobby Woolf · 2003
A catalog of 65 messaging patterns with diagrams - the vocabulary queue designs still use today.
4.6 (581)Contract Testing in Action
Marie Cruz, Lewis Prescott · 2024
Stands up Pact contract tests between consumer and provider and wires them into a CI pipeline.
4.3 (6)Kindle editionRESTful Web API Patterns and Practices Cookbook
Mike Amundsen · 2022
Recipes for hypermedia, retries and service orchestration you can look up mid-implementation.
3.7 (4)
Related articles
Try these templates
Uber Real-Time Location System
Handles 5M+ GPS pings per second using H3 hexagonal geospatial indexing.
6 componentsReal-Time Collaborative Editor
Notion-like document editor with real-time collaboration, conflict resolution, and rich media.
9 componentsNetflix Video Streaming Architecture
Global video streaming platform with adaptive bitrate, CDN distribution, and recommendation engine.
10 components