WebSockets, SSE & Long-Polling
Master real-time communication techniques — HTTP polling, Long-Polling, Server-Sent Events (SSE), and WebSockets — including connection management at scale, when to use each protocol, and how to build a chat system's connection layer.
Master real-time communication techniques — HTTP polling, Long-Polling, Server-Sent Events (SSE), and WebSockets — including connection management at scale, when to use each protocol, and how to build a chat system's connection layer. This hands-on tutorial focuses on practical implementation of websockets, sse & long-polling concepts.
WebSockets, SSE & Long-Polling
When a user sends a message in a chat app, you need to push that message to the recipient's browser immediately — without the recipient constantly asking "any new messages?". This is the real-time push problem, and there are four approaches to solving it, each with different trade-offs.
[!IMPORTANT] WebSockets are not always the right answer. For one-directional server → client streaming (live scores, news feed), SSE is simpler and more scalable. Use WebSockets only when you need true bidirectional communication (chat, collaborative editing, multiplayer games).
Four Real-Time Approaches
WebSockets at Scale: Connection Management
Comparison Table
| Feature | Short Poll | Long Poll | SSE | WebSocket |
|---|---|---|---|---|
| Direction | Client → Server | Client → Server | Server → Client | Bidirectional |
| Latency | Up to poll interval | Near real-time | Real-time | Real-time |
| HTTP overhead | High (per request) | Medium | Low (one connection) | Very low |
| Server load | High | Medium | Low | Medium |
| Firewall friendly | ✅ Always | ✅ Always | ✅ Always | ⚠️ Sometimes blocked |
| Auto-reconnect | Client logic | Client logic | ✅ Built-in | Client logic |
| Best for | Simple, low freq | Notifications | Feeds, streams | Chat, games, collab |
Quiz
Quiz
Question 1 of 2You're building a live sports score ticker that shows real-time match scores to thousands of users. Scores update every 30 seconds. Which protocol is most appropriate?
Key Takeaways
✅ Short polling = simplest but most wasteful; never use in production for real-time features.
✅ Long polling = good middle ground for infrequent updates; requires reconnection after each message.
✅ SSE = best for server-to-client only (news feeds, notifications, progress); built-in auto-reconnect; CDN-friendly.
✅ WebSocket = only when you need true bidirectional communication (chat, collaborative editing, games).
✅ Scale WebSockets with Redis Pub/Sub: each server publishes messages to Redis; all servers subscribe and deliver to their local connections.
Official Resources
- 📖 MDN WebSocket API — Browser WebSocket documentation
- 📖 MDN EventSource (SSE) — Server-Sent Events browser API
- 📖 Socket.io Documentation — WebSocket library with fallbacks, rooms, Redis adapter
- 📖 Redis Pub/Sub — Publish/Subscribe for WebSocket scale-out
What's Next?
With real-time communication mastered, it's time to tackle how large systems are structured at the service level.
Next: Microservices vs Monolith — The architectural trade-offs, when to split, and the Strangler Fig pattern for migration.