Skip to content

WebSockets & Real-Time Communication

Real-time communication means the server pushes data to the client without the client asking — like chat messages, live scores, or notifications.


flowchart TB
RT["Need real-time updates?"] --> O{"How fast?"}
O -->|"Every 5-60s is fine"| Poll["Polling<br/>Client asks every N seconds"]
O -->|"Need low latency"| LP["Long Polling<br/>Server holds request open"]
O -->|"Server→Client only"| SSE["SSE (EventSource)<br/>Server pushes to client"]
O -->|"Bidirectional"| WS["WebSocket<br/>Full duplex"]
style Poll fill:#7c3aed,color:#fff
style LP fill:#4f46e5,color:#fff
style SSE fill:#6366f1,color:#fff
style WS fill:#059669,color:#fff

MethodDirectionLatencyHTTP?ComplexityBrowser Support
PollingClient → ServerSeconds✅ YesLow✅ Universal
Long PollingClient → Server → Client~100ms✅ YesMedium✅ Universal
SSEServer → Client only~10ms✅ YesLowModern browsers
WebSocketBidirectional~10msStarts as HTTP, then upgradesMedium-HighModern browsers

sequenceDiagram
participant Client as 📱 Browser
participant Server as 🖥️ Server
Client->>Server: HTTP GET /ws (Upgrade: websocket)
Server->>Client: 101 Switching Protocols
Note over Client,Server: Connection upgraded to WebSocket
Client->>Server: Send message (chat)
Server->>Client: Push message (notification)
Server->>Client: Push message (message from another user)
Note over Client,Server: Bidirectional, low latency

ScenarioBest Choice
Dashboard polling every 30 secondsPolling — simple, good enough
Real-time stock tickerWebSocket — low latency, constant stream
Live sports scores (on mobile)SSE — server pushes, no upload needed
Collaborative editing (Google Docs)WebSocket — bidirectional, low latency
Notification badge countLong polling — faster than polling, simpler than WS

  • Polling is simple but wasteful — many requests return no data.
  • WebSockets are efficient but need persistent connections (more server resources).
  • SSE is simpler than WebSockets but only works one direction (server → client).
  • For most systems: use polling for non-critical updates, WebSockets for real-time features.

  • Polling = client keeps asking “got anything new?” every N seconds.
  • WebSocket = permanent open connection. Both sides can send data anytime.
  • SSE = server pushes to client only (like live scores, news ticker).
  • WebSocket is the gold standard for real-time chat, gaming, and live updates.