2.4 KiB
2.4 KiB
Feature Specification: WebSocket Chat with Redis Pub/Sub
Overview
Implement a WebSocket endpoint at GET /ws that upgrades HTTP connections to WebSocket for real-time chat functionality. Messages received from clients are published to Redis channels, and a Redis subscriber broadcasts incoming messages to all connected clients.
Requirements
Functional Requirements
-
WebSocket Upgrade Endpoint
GET /api/chat-api/wsupgrades HTTP to WebSocketGET /api/chat-api/ws/{room}joins a specific room on connect- Support optional
?room=query parameter for room selection
-
Message Publishing
- Incoming WebSocket messages are published to Redis pub/sub channels
- Room-specific messages go to
realtime:room:{room}channel - Global messages (no room) go to
realtime:globalchannel
-
Message Broadcasting
- Redis subscriber receives messages from all channels
- Messages are broadcast to all connected WebSocket clients
- Room-targeted messages only go to clients in that room
- Cross-pod broadcasting ensures messages reach all service instances
-
Message Format
{ "id": "uuid", "type": "chat|presence|notification|system", "room": "room-name", "from": "client-id", "data": {}, "timestamp": "2026-02-05T00:00:00Z" }
Non-Functional Requirements
-
Connection Management
- Automatic ping/pong heartbeat (60s timeout)
- Graceful connection cleanup on disconnect
- Maximum message size: 64KB
- Send buffer size: 256 messages
-
Scalability
- Multi-pod deployment via Redis pub/sub
- Pod ID tracking to prevent message echo
-
Configuration
- Redis URL via
REDIS_URLenvironment variable - Auth optional via existing
AUTH_ENABLEDflag
- Redis URL via
Acceptance Criteria
- WebSocket endpoint accessible at
/api/chat-api/ws - Room-based WebSocket endpoint at
/api/chat-api/ws/{room} - Messages published to Redis channels
- Redis subscriber broadcasts to connected clients
- Room-targeted messages only reach room members
- Global messages reach all connected clients
- Connection stats endpoint at
/api/chat-api/ws/stats - Graceful shutdown of WebSocket connections
- Tests cover WebSocket handler and Redis integration
Out of Scope
- Message persistence (database storage)
- Message history retrieval
- User authentication enforcement (auth remains optional)
- Rate limiting
- Message encryption