WebSocketConnection
Defined in: packages/core/src/data/streaming/websocket-connection.ts:62
WebSocket connection for bidirectional streaming.
Remarks
Section titled “Remarks”Provides real-time bidirectional communication with automatic reconnection. Unlike SSE, WebSocket supports sending data from client to server.
Features:
- Full-duplex communication
- JSON message parsing with text fallback
- Manual reconnection with exponential backoff
- Sub-protocol support
- Send capability with connection validation
Example
Section titled “Example”const ws = new WebSocketConnection({ url: 'wss://api.example.com/stream', protocols: 'json',});
ws.on('message', ({ data }) => { console.log('Received:', data);});
await ws.connect();ws.send({ type: 'subscribe', channel: 'updates' });Extends
Section titled “Extends”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new WebSocketConnection(
config):WebSocketConnection
Defined in: packages/core/src/data/streaming/websocket-connection.ts:79
Create a new WebSocket connection.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config | WebSocketConfig | WebSocket configuration |
Returns
Section titled “Returns”WebSocketConnection
Example
Section titled “Example”const connection = new WebSocketConnection({ url: 'wss://api.example.com/stream', protocols: ['json', 'v1'],});Overrides
Section titled “Overrides”Methods
Section titled “Methods”connect()
Section titled “connect()”connect():
Promise<void>
Defined in: packages/core/src/data/streaming/websocket-connection.ts:109
Establish WebSocket connection.
Returns
Section titled “Returns”Promise<void>
Remarks
Section titled “Remarks”Creates a WebSocket and sets up event listeners for:
- Connection open
- Message reception
- Connection close
- Errors
Unlike EventSource, WebSocket does not have built-in reconnection, so reconnection is handled manually via the BaseConnection.
Throws
Section titled “Throws”Error if WebSocket is not supported or connection fails
Example
Section titled “Example”await connection.connect();console.log('Connected to WebSocket');Overrides
Section titled “Overrides”disconnect()
Section titled “disconnect()”disconnect():
void
Defined in: packages/core/src/data/streaming/websocket-connection.ts:176
Close WebSocket connection.
Returns
Section titled “Returns”void
Remarks
Section titled “Remarks”Closes the WebSocket with a normal closure code (1000). Sets the manual disconnect flag to prevent automatic reconnection.
Example
Section titled “Example”connection.disconnect();console.log('Disconnected from WebSocket');Overrides
Section titled “Overrides”eventNames()
Section titled “eventNames()”eventNames(): keyof
ConnectionEvents[]
Defined in: packages/core/src/utils/event-emitter.ts:211
Get all event names that have registered handlers
Returns
Section titled “Returns”keyof ConnectionEvents[]
Array of event names
Example
Section titled “Example”const events = emitter.eventNames();console.log('Events with handlers:', events);Inherited from
Section titled “Inherited from”getReconnectAttempts()
Section titled “getReconnectAttempts()”getReconnectAttempts():
number
Defined in: packages/core/src/data/streaming/base-connection.ts:200
Get the number of reconnection attempts.
Returns
Section titled “Returns”number
Number of reconnect attempts
Inherited from
Section titled “Inherited from”BaseConnection.getReconnectAttempts
getState()
Section titled “getState()”getState():
ConnectionState
Defined in: packages/core/src/data/streaming/base-connection.ts:175
Get current connection state.
Returns
Section titled “Returns”Current state
Example
Section titled “Example”const state = connection.getState();if (state === 'connected') { // Connection is ready}Inherited from
Section titled “Inherited from”hasListeners()
Section titled “hasListeners()”hasListeners<
K>(event):boolean
Defined in: packages/core/src/utils/event-emitter.ts:228
Check if an event has any registered handlers
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends keyof ConnectionEvents |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
event | K | Event name |
Returns
Section titled “Returns”boolean
True if the event has at least one handler
Example
Section titled “Example”if (emitter.hasListeners('message')) { emitter.emit('message', { text: 'Hello' });}Inherited from
Section titled “Inherited from”isConnected()
Section titled “isConnected()”isConnected():
boolean
Defined in: packages/core/src/data/streaming/base-connection.ts:191
Check if connection is currently connected.
Returns
Section titled “Returns”boolean
True if connected
Example
Section titled “Example”if (connection.isConnected()) { connection.send(data);}Inherited from
Section titled “Inherited from”listenerCount()
Section titled “listenerCount()”listenerCount<
K>(event):number
Defined in: packages/core/src/utils/event-emitter.ts:195
Get the number of handlers registered for an event
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends keyof ConnectionEvents |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
event | K | Event name |
Returns
Section titled “Returns”number
Number of registered handlers
Example
Section titled “Example”const count = emitter.listenerCount('message');console.log(`${count} handlers registered`);Inherited from
Section titled “Inherited from”off<
K>(event,handler):void
Defined in: packages/core/src/utils/event-emitter.ts:120
Remove an event handler
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends keyof ConnectionEvents |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
event | K | Event name |
handler | EventHandler<ConnectionEvents[K]> | Handler function to remove |
Returns
Section titled “Returns”void
Example
Section titled “Example”const handler = (data) => console.log(data);emitter.on('message', handler);emitter.off('message', handler);Inherited from
Section titled “Inherited from”on<
K>(event,handler): () =>void
Defined in: packages/core/src/utils/event-emitter.ts:65
Register an event handler
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends keyof ConnectionEvents |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
event | K | Event name to listen for |
handler | EventHandler<ConnectionEvents[K]> | Callback function to invoke when event is emitted |
Returns
Section titled “Returns”Unsubscribe function that removes this specific handler
() => void
Example
Section titled “Example”const unsubscribe = emitter.on('message', (data) => { console.log(data.text);});
// Later, to unsubscribe:unsubscribe();Inherited from
Section titled “Inherited from”once()
Section titled “once()”once<
K>(event,handler):void
Defined in: packages/core/src/utils/event-emitter.ts:95
Register a one-time event handler
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends keyof ConnectionEvents |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
event | K | Event name to listen for |
handler | EventHandler<ConnectionEvents[K]> | Callback function to invoke once |
Returns
Section titled “Returns”void
Remarks
Section titled “Remarks”The handler will be automatically removed after being invoked once.
Example
Section titled “Example”emitter.once('connect', () => { console.log('Connected!');});Inherited from
Section titled “Inherited from”removeAllListeners()
Section titled “removeAllListeners()”removeAllListeners<
K>(event?):void
Defined in: packages/core/src/utils/event-emitter.ts:175
Remove all handlers for an event, or all handlers for all events
Type Parameters
Section titled “Type Parameters”| Type Parameter |
|---|
K extends keyof ConnectionEvents |
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
event? | K | Optional event name. If omitted, removes all handlers for all events. |
Returns
Section titled “Returns”void
Example
Section titled “Example”// Remove all handlers for 'message' eventemitter.removeAllListeners('message');
// Remove all handlers for all eventsemitter.removeAllListeners();Inherited from
Section titled “Inherited from”BaseConnection.removeAllListeners
send()
Section titled “send()”send(
data):void
Defined in: packages/core/src/data/streaming/websocket-connection.ts:198
Send data through WebSocket.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
data | unknown | Data to send (will be JSON stringified) |
Returns
Section titled “Returns”void
Throws
Section titled “Throws”Error if not connected
Remarks
Section titled “Remarks”The data is automatically converted to JSON before sending. Throws an error if called when the connection is not established.
Example
Section titled “Example”connection.send({ type: 'ping' });connection.send({ type: 'subscribe', channel: 'updates' });