StreamManager
Defined in: packages/core/src/data/streaming/stream-manager.ts:124
Manages streaming connections (SSE and WebSocket).
Remarks
Section titled “Remarks”Provides a unified interface for managing multiple streaming connections, handling connection lifecycle, automatic reconnection, and message routing.
Features:
- Multiple concurrent connections
- Automatic reconnection with exponential backoff
- Connection state tracking
- Message counting and statistics
- Type-safe callbacks
Example
Section titled “Example”const manager = new StreamManager();
// Connect to SSE streamawait manager.connect('earthquake-feed', { type: 'sse', url: 'https://earthquake.usgs.gov/events', onData: (data) => updateMap(data), reconnect: { enabled: true }});
// Connect to WebSocket streamawait manager.connect('vehicle-updates', { type: 'websocket', url: 'wss://transit.example.com/vehicles', onData: (data) => updateVehicles(data), protocols: ['json']});
// Send data via WebSocketmanager.send('vehicle-updates', { type: 'subscribe', channel: 'all' });
// Check connection stateconst state = manager.getState('earthquake-feed');console.log(`Messages received: ${state?.messageCount}`);
// Disconnect when donemanager.disconnect('earthquake-feed');manager.disconnectAll();Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new StreamManager():
StreamManager
Returns
Section titled “Returns”StreamManager
Methods
Section titled “Methods”connect()
Section titled “connect()”connect(
id,config):Promise<void>
Defined in: packages/core/src/data/streaming/stream-manager.ts:143
Connect to a streaming source.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
id | string | Unique identifier for this connection |
config | StreamConfig | Stream configuration |
Returns
Section titled “Returns”Promise<void>
Throws
Section titled “Throws”If a connection with the given id already exists
Example
Section titled “Example”await manager.connect('updates', { type: 'sse', url: 'https://api.example.com/stream', onData: (data) => console.log(data)});destroy()
Section titled “destroy()”destroy():
void
Defined in: packages/core/src/data/streaming/stream-manager.ts:292
Clean up all resources.
Returns
Section titled “Returns”void
Example
Section titled “Example”manager.destroy();disconnect()
Section titled “disconnect()”disconnect(
id):void
Defined in: packages/core/src/data/streaming/stream-manager.ts:179
Disconnect a specific stream.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
id | string | Stream identifier |
Returns
Section titled “Returns”void
Example
Section titled “Example”manager.disconnect('updates');disconnectAll()
Section titled “disconnectAll()”disconnectAll():
void
Defined in: packages/core/src/data/streaming/stream-manager.ts:197
Disconnect all active streams.
Returns
Section titled “Returns”void
Example
Section titled “Example”manager.disconnectAll();getActiveIds()
Section titled “getActiveIds()”getActiveIds():
string[]
Defined in: packages/core/src/data/streaming/stream-manager.ts:252
Get all active stream IDs.
Returns
Section titled “Returns”string[]
Array of active stream identifiers
Example
Section titled “Example”const activeStreams = manager.getActiveIds();console.log(`Active streams: ${activeStreams.join(', ')}`);getState()
Section titled “getState()”getState(
id):StreamState|null
Defined in: packages/core/src/data/streaming/stream-manager.ts:218
Get the current state of a stream.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
id | string | Stream identifier |
Returns
Section titled “Returns”StreamState | null
Stream state or null if not found
Example
Section titled “Example”const state = manager.getState('updates');if (state) { console.log(`State: ${state.connectionState}`); console.log(`Messages: ${state.messageCount}`);}isConnected()
Section titled “isConnected()”isConnected(
id):boolean
Defined in: packages/core/src/data/streaming/stream-manager.ts:236
Check if a stream is currently connected.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
id | string | Stream identifier |
Returns
Section titled “Returns”boolean
True if connected, false otherwise
Example
Section titled “Example”if (manager.isConnected('updates')) { console.log('Stream is active');}send()
Section titled “send()”send(
id,data):void
Defined in: packages/core/src/data/streaming/stream-manager.ts:271
Send data to a WebSocket connection.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
id | string | Stream identifier |
data | unknown | Data to send (will be JSON stringified) |
Returns
Section titled “Returns”void
Throws
Section titled “Throws”If stream is not a WebSocket connection or not connected
Example
Section titled “Example”manager.send('ws-updates', { type: 'subscribe', channels: ['news', 'sports']});