3.5 KiB
3.5 KiB
Implementation Tasks: WebSocket Chat with Redis Pub/Sub
Task 1: Add Redis Configuration
ID: redis-config
Scope: Add Redis URL to service configuration
Files:
services/chat-api/internal/config/config.goservices/chat-api/.env.example
Changes:
- Add
RedisURL stringfield to Config struct - Read from
REDIS_URLenvironment variable - Add
REDIS_URLto.env.examplewith comment
Acceptance:
- Config struct has RedisURL field
- Load() reads REDIS_URL from environment
- .env.example documents the configuration
Task 2: Initialize Hub and Broadcaster
ID: hub-init
Blocked By: redis-config
Scope: Initialize realtime components in main.go
Files:
services/chat-api/cmd/server/main.go
Changes:
- Add context with cancel for graceful shutdown
- Create LocalHub and start event loop
- Create RedisBroadcaster if RedisURL configured
- Update RegisterRoutes call to pass hub and broadcaster
- Register shutdown hook to cancel context
Acceptance:
- Hub event loop runs in goroutine
- Redis broadcaster runs if configured
- Context cancelled on shutdown
- Service starts without errors
Task 3: Mount WebSocket Handler
ID: ws-routes
Blocked By: hub-init
Scope: Register WebSocket routes in routes.go
Files:
services/chat-api/internal/api/routes.go
Changes:
- Update RegisterRoutes signature to accept Hub and Broadcaster
- Create realtime.Handler with configuration
- Mount handler at
/wspath under/api/chat-api - Add stats endpoint at
/ws/stats
Acceptance:
- WebSocket upgrade works at
/api/chat-api/ws - Room-based endpoint at
/api/chat-api/ws/{room} - Stats endpoint returns connection count
- Auth middleware applied when AUTH_ENABLED=true
Task 4: Add WebSocket OpenAPI Documentation
ID: ws-openapi
Blocked By: ws-routes
Scope: Document WebSocket endpoints in OpenAPI spec
Files:
services/chat-api/internal/api/spec.go
Changes:
- Add
/wsendpoint documentation (upgrade endpoint) - Add
/ws/{room}endpoint documentation - Add
/ws/statsendpoint with response schema - Document WebSocket message format in schemas
Acceptance:
- OpenAPI spec includes WebSocket endpoints
- Stats endpoint has proper response schema
- Message format documented
Task 5: Create WebSocket Integration Tests
ID: ws-tests
Blocked By: ws-routes
Scope: Test WebSocket handler functionality
Files:
services/chat-api/internal/api/handlers/ws_test.go
Tests:
- WebSocket connection upgrade succeeds
- Room join via URL parameter works
- Room join via query parameter works
- Message broadcast to room members
- Global message broadcast to all clients
- Stats endpoint returns correct counts
- Connection cleanup on disconnect
Acceptance:
- All tests pass
- Coverage includes happy path and error cases
- Tests use httptest for server simulation
Task Summary
| ID | Task | Blocked By | Status |
|---|---|---|---|
redis-config |
Add Redis Configuration | - | pending |
hub-init |
Initialize Hub and Broadcaster | redis-config | pending |
ws-routes |
Mount WebSocket Handler | hub-init | pending |
ws-openapi |
Add WebSocket OpenAPI Documentation | ws-routes | pending |
ws-tests |
Create WebSocket Integration Tests | ws-routes | pending |
Dependency Graph
redis-config
│
▼
hub-init
│
▼
ws-routes
│
├─────────┐
▼ ▼
ws-openapi ws-tests