Script Valley
WebSockets & Real-Time Applications
Scaling WebSocket Servers/Assessment

Practice & Assessment

Test your understanding of Scaling WebSocket Servers

Multiple Choice Questions

6
1

Why does a standard round-robin load balancer break WebSocket broadcasting?

2

Why does the Socket.IO Redis adapter require two separate Redis client connections?

3

What is the main limitation of using sticky sessions as your only WebSocket scaling strategy?

4

What does a sudden sharp drop in the connected_clients gauge most likely indicate?

5

In the Redis pub/sub pattern for WebSocket scaling, what is the purpose of including an originServer field in published messages?

6

Which Nginx directive prevents a load balancer from closing a long-lived WebSocket connection due to inactivity?

Coding Challenges

1
1

Implement Cross-Server WebSocket Broadcast with Redis

Start two Node.js WebSocket servers on ports 8081 and 8082 using the ws package. Both servers should subscribe to a Redis channel named 'ws:broadcast' using ioredis. When any client connected to either server sends a { type: 'broadcast', text: string } message, the receiving server publishes it to Redis with an originServer field set to its own port. Both servers listen on the channel and forward received messages to their local clients, but skip messages where originServer matches their own port to avoid double delivery. Demonstrate this works by connecting two test clients to different ports and verifying a message from one reaches the other. Input: WebSocket messages from clients. Output: broadcast received on both servers' clients. Estimated time: 30 minutes.

Hard

Mini Project

1

Horizontally Scalable Chat Server with Redis and Metrics

Build a production-ready chat server that can run as multiple instances. Requirements: Node.js with the ws package and ioredis for pub/sub; global broadcast channel (ws:broadcast) and per-room channels (ws:room:roomId) โ€” servers subscribe only to rooms that have active local clients and unsubscribe when a room empties; users join rooms by sending { type: 'join', room: string, username: string }; messages sent to a room are published to the room's Redis channel with originServer tagged; all instances receive and deliver to local room members; Nginx configuration file (nginx.conf) with ip_hash sticky sessions and WebSocket proxy headers; a /metrics HTTP endpoint exporting Prometheus-format gauges for ws_connected_clients and counters for ws_messages_received_total and ws_messages_sent_total; a README with Docker Compose setup running two server instances, Nginx, and Redis, with a curl command to verify /metrics. Difficulty is hard due to the operational complexity of the multi-process setup.

Hard
Practice & Assessment โ€” Scaling WebSocket Servers โ€” WebSockets & Real-Time Applications โ€” Script Valley โ€” Script Valley