MATIH Platform is in active MVP development. Documentation reflects current implementation status.
15. Workbench Architecture
Shared Library Deep Dive
Real-Time Infrastructure

Real-Time Infrastructure

The shared real-time module at frontend/shared/src/realtime/ provides WebSocket connection management, Server-Sent Events for streaming, presence tracking, live notifications, and query status updates. All workbenches use these primitives for real-time features.


Module Structure

realtime/
  WebSocketProvider.tsx    # WebSocket connection with auto-reconnect
  EventSource.tsx          # SSE streaming provider
  Presence.tsx             # User presence tracking
  NotificationCenter.tsx   # Real-time notification delivery
  LiveUpdates.tsx          # Live data subscription
  queryStatus.ts           # Query execution status tracking
  types.ts                 # Shared type definitions

WebSocketProvider

File: frontend/shared/src/realtime/WebSocketProvider.tsx

Provides WebSocket connection management with auto-reconnect, message queuing, and channel-based pub/sub.

Configuration

interface WebSocketConfig {
  url: string;
  protocols?: string[];
  headers?: Record<string, string>;
  authToken?: string;
  autoReconnect?: boolean;          // Default: true
  maxReconnectAttempts?: number;    // Default: 10
  reconnectDelay?: number;          // Default: 1000ms
  reconnectDelayMultiplier?: number; // Default: 1.5
  maxReconnectDelay?: number;       // Default: 30000ms
  pingInterval?: number;            // Default: 30000ms
  queueSize?: number;               // Default: 100
}

Connection Status

type WebSocketStatus = 'connecting' | 'connected' | 'disconnected' | 'reconnecting' | 'error';

Context API

interface WebSocketContextType {
  status: WebSocketStatus;
  send: <T>(message: WebSocketMessage<T>) => void;
  subscribe: <T>(channel: string, handler: (data: T) => void) => () => void;
  unsubscribe: (channel: string) => void;
  connect: () => void;
  disconnect: () => void;
  reconnect: () => void;
}

Usage

<WebSocketProvider config={{
  url: 'wss://api.matih.ai/ws',
  authToken: 'session-token',
  pingInterval: 30000,
}}>
  <App />
</WebSocketProvider>

Hooks

// Access full WebSocket context
const { status, send, subscribe } = useWebSocket();
 
// Subscribe to a channel with automatic cleanup
useWebSocketChannel<DashboardUpdate>('dashboard:updates', (data) => {
  updateDashboard(data);
});
 
// Get connection status
const status = useWebSocketStatus();

Auto-Reconnect Strategy

Uses exponential backoff with a configurable multiplier:

AttemptDelay
11000ms
21500ms
32250ms
43375ms
...max 30000ms

Message Queuing

When the WebSocket is disconnected, messages are queued (up to queueSize messages) and flushed when the connection is re-established.

Ping/Pong

The provider sends periodic ping messages to keep the connection alive:

wsRef.current.send(JSON.stringify({ type: 'ping', timestamp: Date.now() }));

Pong responses are silently consumed without dispatching to subscribers.

Presence Tracking

File: frontend/shared/src/realtime/Presence.tsx

Tracks which users are active on which resources for collaboration features.

NotificationCenter

File: frontend/shared/src/realtime/NotificationCenter.tsx

Manages real-time notification delivery and display.

LiveUpdates

File: frontend/shared/src/realtime/LiveUpdates.tsx

Provides live data subscription for dashboard refresh and real-time data feeds.

Query Status Tracking

File: frontend/shared/src/realtime/queryStatus.ts

Tracks SQL query execution status in real-time, providing progress updates for long-running queries.