Problem
Add real-time capabilities to an existing REST API application. Users should receive instant updates when data changes, without polling. Specifically, implement a notification system where users see new notifications appear in real-time, and a live activity feed that updates as other users perform actions.
Requirements
- WebSocket Connection: Clients establish a persistent WebSocket connection on login.
- User-Specific Channels: Each user receives only their own notifications (private channel).
- Shared Channels: Multiple users can subscribe to a shared channel (e.g., a project activity feed).
- Event Types: Support different event types:
notification:new, activity:new, data:updated, user:presence.
- Connection Management: Handle reconnection, heartbeats, and authentication.
- Horizontal Scaling: The WebSocket server must work across multiple application instances (users connected to different servers can still communicate).
Constraints
- The existing REST API is built with Express.js / Next.js.
- Up to 10,000 concurrent WebSocket connections.
- Messages must be delivered within 500ms of the triggering event.
- The system must handle disconnections gracefully (queue messages for brief disconnections).
- Authentication must be validated on WebSocket connection, not just on the initial HTTP upgrade.
What to Design
- The WebSocket server architecture and integration with the existing HTTP server
- The authentication and authorization flow for WebSocket connections
- The pub/sub mechanism for broadcasting events across server instances
- The message queuing strategy for temporarily disconnected users
- How REST API endpoints trigger WebSocket events