Skip to content

[Backend] WebSocket Real-Time Channel Server with Heartbeat & Room Multiplexing #1446

Description

@blurbeast

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

  1. Unidirectional Limitations:
    • Clients cannot dynamically subscribe to new streams or update filter parameters over an open SSE connection.
  2. Silent Connection Dropping:
    • Firewalls and reverse proxies terminate idle SSE streams without either party knowing, causing stale UI balances.
  3. 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

  • WebSocket clients can connect, authenticate, and join/leave subscription rooms.
  • On-chain stream events are broadcasted to subscribed WebSocket rooms with <100ms latency.
  • Automatic ping/pong terminates dead sockets cleanly and frees server memory.
  • Unit tests verify multi-room broadcasting and reconnection resilience.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions