BaseConnection
Defined in: packages/core/src/data/streaming/base-connection.ts:105
Abstract base class for streaming connections.
Remarks
Section titled “Remarks”Provides common functionality for all streaming connection types:
- Connection state management
- Event emission via EventEmitter
- Automatic reconnection with exponential backoff
- State change tracking
Subclasses must implement:
connect(): Establish the connectiondisconnect(): Close the connection
Example
Section titled “Example”class MyConnection extends BaseConnection { async connect(): Promise<void> { this.setState('connecting'); // ... connection logic this.setState('connected'); this.emit('connect', undefined); }
disconnect(): void { // ... disconnection logic this.handleDisconnect('Manual disconnect'); }}Extends
Section titled “Extends”Extended by
Section titled “Extended by”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new BaseConnection(
config):BaseConnection
Defined in: packages/core/src/data/streaming/base-connection.ts:117
Create a new base connection.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config | ConnectionConfig | Connection configuration |
Returns
Section titled “Returns”BaseConnection
Overrides
Section titled “Overrides”Methods
Section titled “Methods”connect()
Section titled “connect()”
abstractconnect():Promise<void>
Defined in: packages/core/src/data/streaming/base-connection.ts:150
Establish connection to the server.
Returns
Section titled “Returns”Promise<void>
Remarks
Section titled “Remarks”Must be implemented by subclasses. Should:
- Set state to ‘connecting’
- Establish the connection
- Set state to ‘connected’
- Emit ‘connect’ event
Throws
Section titled “Throws”Error if connection fails
disconnect()
Section titled “disconnect()”
abstractdisconnect():void
Defined in: packages/core/src/data/streaming/base-connection.ts:160
Close the connection.
Returns
Section titled “Returns”void
Remarks
Section titled “Remarks”Must be implemented by subclasses. Should:
- Close the underlying connection
- Call handleDisconnect() with reason
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
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}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);}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();