SSEConnection
Defined in: packages/core/src/data/streaming/sse-connection.ts:76
Server-Sent Events connection.
Remarks
Section titled “Remarks”Primary streaming mechanism for real-time updates. Uses the native EventSource API for robust, automatic reconnection handling.
Features:
- Automatic reconnection by the browser
- Event-based message streaming
- JSON message parsing with error handling
- Multiple event type support
- Last event ID tracking for resume
Example
Section titled “Example”const sse = new SSEConnection({ url: 'https://api.example.com/events', eventTypes: ['update', 'delete'],});
sse.on('message', ({ data }) => { console.log('Received:', data);});
sse.on('error', ({ error }) => { console.error('Error:', error);});
await sse.connect();Extends
Section titled “Extends”Constructors
Section titled “Constructors”Constructor
Section titled “Constructor”new SSEConnection(
config):SSEConnection
Defined in: packages/core/src/data/streaming/sse-connection.ts:95
Create a new SSE connection.
Parameters
Section titled “Parameters”| Parameter | Type | Description |
|---|---|---|
config | SSEConfig | SSE configuration |
Returns
Section titled “Returns”SSEConnection
Example
Section titled “Example”const connection = new SSEConnection({ url: 'https://api.example.com/stream', eventTypes: ['message', 'update'], withCredentials: true,});Overrides
Section titled “Overrides”Methods
Section titled “Methods”connect()
Section titled “connect()”connect():
Promise<void>
Defined in: packages/core/src/data/streaming/sse-connection.ts:125
Establish SSE connection.
Returns
Section titled “Returns”Promise<void>
Remarks
Section titled “Remarks”Creates an EventSource and sets up event listeners for:
- Connection open
- Message events (for each configured event type)
- Error events
The EventSource API handles reconnection automatically when the connection is lost, unless explicitly closed.
Throws
Section titled “Throws”Error if EventSource is not supported or connection fails
Example
Section titled “Example”await connection.connect();console.log('Connected to SSE stream');Overrides
Section titled “Overrides”disconnect()
Section titled “disconnect()”disconnect():
void
Defined in: packages/core/src/data/streaming/sse-connection.ts:190
Close SSE connection.
Returns
Section titled “Returns”void
Remarks
Section titled “Remarks”Closes the EventSource and cleans up all event listeners. Sets the manual disconnect flag to prevent automatic reconnection.
Example
Section titled “Example”connection.disconnect();console.log('Disconnected from SSE stream');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”getLastEventId()
Section titled “getLastEventId()”getLastEventId():
string|null
Defined in: packages/core/src/data/streaming/sse-connection.ts:214
Get the last event ID received from the server.
Returns
Section titled “Returns”string | null
Last event ID or null if none received
Remarks
Section titled “Remarks”The event ID is used by the EventSource API to resume the stream
from the last received event after a reconnection. The browser
automatically sends this ID in the Last-Event-ID header.
Example
Section titled “Example”const lastId = connection.getLastEventId();if (lastId) { console.log(`Last event: ${lastId}`);}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();