Overview & Background
FlowFi currently provides real-time stream state updates using Server-Sent Events (SSE). While SSE works well for simple one-way feeds, it is constrained by HTTP/1.1 per-domain connection limits (typically 6 concurrent connections in browsers), cannot receive client commands (such as dynamic subscription filter updates without reconnecting), and experiences silent disconnections behind aggressive cloud load balancers. Adding a WebSocket server provides bidirectional multiplexing, active ping/pong keepalives, and room-based wallet subscription channels.
Detailed Problem Statement
- Unidirectional Limitations:
- Clients cannot dynamically subscribe to new streams or update filter parameters over an open SSE connection.
- Silent Connection Dropping:
- Firewalls and reverse proxies terminate idle SSE streams without either party knowing, causing stale UI balances.
- High Socket Overhead:
- A user monitoring multiple dashboard views must maintain multiple independent SSE connections.
Technical Specification & Architecture
1. WebSocket Server Architecture
Implement backend/src/services/websocket.service.ts using ws:
- Attaches to the main Node.js HTTP server.
- Supports bidirectional framing with JSON-RPC-style messages:
// Client -> Server Subscribe
{ "action": "subscribe", "topic": "streams", "wallet": "G...", "streamId": "123" }
// Client -> Server Ping
{ "action": "ping" }
// Server -> Client Stream Event
{ "event": "STREAM_UPDATED", "streamId": "123", "data": { ... } }
2. Redis Pub/Sub Integration
- Multiplex incoming Redis stream events to corresponding WebSocket client rooms.
- Cleanly map wallet addresses to connected WebSocket client sets.
3. Heartbeat & Stale Connection Pruning
- Broadcast
ping every 30 seconds; terminate sockets that fail to return pong within 10 seconds.
Target Files
backend/src/services/websocket.service.ts
backend/src/app.ts
backend/src/index.ts
backend/tests/websocket.service.test.ts
Acceptance Criteria
Overview & Background
FlowFi currently provides real-time stream state updates using Server-Sent Events (SSE). While SSE works well for simple one-way feeds, it is constrained by HTTP/1.1 per-domain connection limits (typically 6 concurrent connections in browsers), cannot receive client commands (such as dynamic subscription filter updates without reconnecting), and experiences silent disconnections behind aggressive cloud load balancers. Adding a WebSocket server provides bidirectional multiplexing, active ping/pong keepalives, and room-based wallet subscription channels.
Detailed Problem Statement
Technical Specification & Architecture
1. WebSocket Server Architecture
Implement
backend/src/services/websocket.service.tsusingws:2. Redis Pub/Sub Integration
3. Heartbeat & Stale Connection Pruning
pingevery 30 seconds; terminate sockets that fail to returnpongwithin 10 seconds.Target Files
backend/src/services/websocket.service.tsbackend/src/app.tsbackend/src/index.tsbackend/tests/websocket.service.test.tsAcceptance Criteria