Polling vs push
| Aspect | Short polling | WebSocket |
|---|---|---|
| Connection | New HTTP request every N seconds | One long-lived TCP connection |
| Latency | Up to poll interval (e.g. 5s) | Sub-second server push |
| Overhead | High — repeated headers | Low after handshake |
| Server load | Scales with poll frequency × users | Stateful connections per user |
Connection
Short pollingNew HTTP request every N secondsWebSocketOne long-lived TCP connectionLatency
Short pollingUp to poll interval (e.g. 5s)WebSocketSub-second server pushOverhead
Short pollingHigh — repeated headersWebSocketLow after handshakeServer load
Short pollingScales with poll frequency × usersWebSocketStateful connections per user
Long polling and SSE sit between these extremes; WebSocket wins for bidirectional real-time.
WebSocket handshake
WebSockets start as HTTP, then upgrade:
GET /ws/live/chat HTTP/1.1
Host: live.streamhub.com
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Key: dGhlIHNhbXBsZSBub25jZQ==
Sec-WebSocket-Version: 13
HTTP/1.1 101 Switching Protocols
Upgrade: websocket
Connection: Upgrade
Sec-WebSocket-Accept: s3pPLMBiTxaQ9kYGzzhZRbK+xOo=
After 101, both sides send framed messages — text or binary — without new HTTP requests.
Client
Serverpersistent socket
Clientreal-time update
Message protocol design
Define a schema so clients and servers agree on event types:
// Client → server: join live room
{ "type": "join", "room_id": "live_v_9182", "token": "eyJ..." }
// Server → client: viewer count update
{ "type": "viewer_count", "room_id": "live_v_9182", "count": 48291 }
// Server → client: new chat message
{ "type": "chat", "user": "creator_42", "text": "Thanks for watching!", "ts": 1712001234 }
Use JSON for readability in early products; protobuf or MessagePack at extreme scale.
The rest of this guide is part of Pro
Free covers every fresher guide in full. Pro unlocks Senior, Staff and Architect guides across every track.
Still to come · 4 sections
- Scaling WebSocket servers
- StreamHub live features
- Alternatives worth mentioning
- Reliability concerns
Test yourself
Answer these before moving on — recall is what makes it stick.