Skip to content

SSEConnection

Defined in: packages/core/src/data/streaming/sse-connection.ts:76

Server-Sent Events connection.

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
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();

new SSEConnection(config): SSEConnection

Defined in: packages/core/src/data/streaming/sse-connection.ts:95

Create a new SSE connection.

ParameterTypeDescription
configSSEConfigSSE configuration

SSEConnection

const connection = new SSEConnection({
url: 'https://api.example.com/stream',
eventTypes: ['message', 'update'],
withCredentials: true,
});

BaseConnection.constructor

connect(): Promise<void>

Defined in: packages/core/src/data/streaming/sse-connection.ts:125

Establish SSE connection.

Promise<void>

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.

Error if EventSource is not supported or connection fails

await connection.connect();
console.log('Connected to SSE stream');

BaseConnection.connect


disconnect(): void

Defined in: packages/core/src/data/streaming/sse-connection.ts:190

Close SSE connection.

void

Closes the EventSource and cleans up all event listeners. Sets the manual disconnect flag to prevent automatic reconnection.

connection.disconnect();
console.log('Disconnected from SSE stream');

BaseConnection.disconnect


eventNames(): keyof ConnectionEvents[]

Defined in: packages/core/src/utils/event-emitter.ts:211

Get all event names that have registered handlers

keyof ConnectionEvents[]

Array of event names

const events = emitter.eventNames();
console.log('Events with handlers:', events);

BaseConnection.eventNames


getLastEventId(): string | null

Defined in: packages/core/src/data/streaming/sse-connection.ts:214

Get the last event ID received from the server.

string | null

Last event ID or null if none received

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.

const lastId = connection.getLastEventId();
if (lastId) {
console.log(`Last event: ${lastId}`);
}

getReconnectAttempts(): number

Defined in: packages/core/src/data/streaming/base-connection.ts:200

Get the number of reconnection attempts.

number

Number of reconnect attempts

BaseConnection.getReconnectAttempts


getState(): ConnectionState

Defined in: packages/core/src/data/streaming/base-connection.ts:175

Get current connection state.

ConnectionState

Current state

const state = connection.getState();
if (state === 'connected') {
// Connection is ready
}

BaseConnection.getState


hasListeners<K>(event): boolean

Defined in: packages/core/src/utils/event-emitter.ts:228

Check if an event has any registered handlers

Type Parameter
K extends keyof ConnectionEvents
ParameterTypeDescription
eventKEvent name

boolean

True if the event has at least one handler

if (emitter.hasListeners('message')) {
emitter.emit('message', { text: 'Hello' });
}

BaseConnection.hasListeners


isConnected(): boolean

Defined in: packages/core/src/data/streaming/base-connection.ts:191

Check if connection is currently connected.

boolean

True if connected

if (connection.isConnected()) {
connection.send(data);
}

BaseConnection.isConnected


listenerCount<K>(event): number

Defined in: packages/core/src/utils/event-emitter.ts:195

Get the number of handlers registered for an event

Type Parameter
K extends keyof ConnectionEvents
ParameterTypeDescription
eventKEvent name

number

Number of registered handlers

const count = emitter.listenerCount('message');
console.log(`${count} handlers registered`);

BaseConnection.listenerCount


off<K>(event, handler): void

Defined in: packages/core/src/utils/event-emitter.ts:120

Remove an event handler

Type Parameter
K extends keyof ConnectionEvents
ParameterTypeDescription
eventKEvent name
handlerEventHandler<ConnectionEvents[K]>Handler function to remove

void

const handler = (data) => console.log(data);
emitter.on('message', handler);
emitter.off('message', handler);

BaseConnection.off


on<K>(event, handler): () => void

Defined in: packages/core/src/utils/event-emitter.ts:65

Register an event handler

Type Parameter
K extends keyof ConnectionEvents
ParameterTypeDescription
eventKEvent name to listen for
handlerEventHandler<ConnectionEvents[K]>Callback function to invoke when event is emitted

Unsubscribe function that removes this specific handler

() => void

const unsubscribe = emitter.on('message', (data) => {
console.log(data.text);
});
// Later, to unsubscribe:
unsubscribe();

BaseConnection.on


once<K>(event, handler): void

Defined in: packages/core/src/utils/event-emitter.ts:95

Register a one-time event handler

Type Parameter
K extends keyof ConnectionEvents
ParameterTypeDescription
eventKEvent name to listen for
handlerEventHandler<ConnectionEvents[K]>Callback function to invoke once

void

The handler will be automatically removed after being invoked once.

emitter.once('connect', () => {
console.log('Connected!');
});

BaseConnection.once


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 Parameter
K extends keyof ConnectionEvents
ParameterTypeDescription
event?KOptional event name. If omitted, removes all handlers for all events.

void

// Remove all handlers for 'message' event
emitter.removeAllListeners('message');
// Remove all handlers for all events
emitter.removeAllListeners();

BaseConnection.removeAllListeners