Server-Sent Events & Scaling Realtime
Overview
This tutorial covers Server-Sent Events (SSE) as an alternative to WebSockets for one‑way real‑time communication. You'll also learn how to scale real‑time applications using Redis Pub/Sub and horizontal scaling strategies for Socket.io servers.
1. Server-Sent Events (SSE)
SSE allows a server to push events to clients over HTTP. It's simpler than WebSockets for one‑way communication.
Server (Node.js)
Client (browser)
2. SSE vs WebSockets
| Feature | SSE | WebSocket |
|---|---|---|
| Communication | Server → Client only | Bidirectional |
| Protocol | HTTP | WS/WSS |
| Reconnection | Automatic | Manual (Socket.io handles) |
| Binary data | Limited (Base64) | Full support |
| Use cases | Live scores, news feeds, notifications | Chat, gaming, collaborative tools |
SSE is simpler and works over HTTP, making it easier to implement and scale.
3. Redis Pub/Sub for Scaling
Redis Pub/Sub allows Socket.io servers to share messages across multiple instances, enabling horizontal scaling.
4. Horizontal Scaling Strategies
Sticky sessions
Some applications require sticky sessions to maintain state. Use a load balancer that supports session affinity.
Redis for state sharing
Use Redis to store session data, room memberships, and other state that needs to be shared across instances.
Database-backed state
For persistent state (e.g., message history), use a database like PostgreSQL or MongoDB instead of in‑memory storage.
- Use Redis Pub/Sub for inter‑instance communication
- Store room data in Redis (not memory)
- Use a load balancer with WebSocket support
- Monitor connection count and scale horizontally
Quiz
Question 1
What is the primary advantage of SSE over WebSockets?
- Bidirectional communication
- Simplicity and built‑in reconnection
- Binary data support
- Lower latency
Show answer
Question 2
Which Redis feature enables Socket.io scaling?
- Redis Cache
- Redis Pub/Sub
- Redis Streams
- Redis Lists
Show answer
Question 3
What is the MIME type for Server-Sent Events?
- text/plain
- text/event-stream
- application/json
- application/octet-stream
Show answer
Exercises
Exercise 1
Implement an SSE endpoint that streams random stock prices for 3 different stocks every 500ms.
Sample answer
Exercise 2
Configure Socket.io with Redis adapter for horizontal scaling.
Sample answer
Homework
Homework 1
Scale the chat application from Tutorial 1 using Redis Pub/Sub. Deploy two instances of the server and verify that messages are synchronised between them.
Sample outline
- Setup: Install Redis and Socket.io Redis adapter.
- Configure: Use the Redis adapter for Socket.io.
- Run: Start two server instances on different ports.
- Test: Connect from two clients to different servers and verify messages are broadcast to both.
Mini‑Project
Scalable Notification Service
Build a scalable notification service that:
- Uses SSE for one‑way real‑time notifications
- Uses Redis Pub/Sub for distributing notifications across multiple server instances
- Stores notification history in a database (PostgreSQL)
- Can scale horizontally with multiple instances
Sample outline
- SSE endpoint: /api/notifications/stream
- Redis: Pub/Sub for inter‑instance communication
- Database: PostgreSQL with Prisma for notification history
- Publisher: Any instance can publish notifications
- Consumer: Each instance subscribes and pushes to connected clients
Tutorial Summary
You learned Server-Sent Events (SSE) as a simpler alternative to WebSockets for one‑way real‑time communication. You also mastered scaling real‑time applications using Redis Pub/Sub and horizontal scaling strategies. These skills are essential for building production‑grade real‑time systems.
Key takeaway: Choose the right tool for the job: SSE for one‑way streaming, WebSockets for bidirectional. Always plan for scaling from the start using Redis and stateless design.