@egain/ai-agent-sdk API Reference - v0.1.1 / Connection
Class: Connection
Connection manager with automatic reconnection.
This class wraps a Transport implementation and adds:
- Connection state management
- Automatic reconnection with exponential backoff
- Event forwarding and normalization
The Connection class is transport-agnostic - it can work with any transport that implements the Transport interface (WebSocket, SSE, etc.)
Example
// Using default WebSocket transport
const conn = new Connection({
endpoint: 'wss://api.example.com/ws'
});
// Using custom transport
const customTransport = new MyCustomTransport({ endpoint: '...' });
const conn = new Connection({ transport: customTransport });Hierarchy
EventEmitter<ConnectionEvents>↳
Connection
Table of contents
Constructors
Methods
- getTransport
- getTransportType
- getState
- isConnected
- connect
- disconnect
- send
- setMaxReconnectAttempts
- setReconnectDelays
- on
- once
- off
- removeAllListeners
- listenerCount
Constructors
constructor
• new Connection(config): Connection
Parameters
| Name | Type |
|---|---|
config | ConnectionConfig |
Returns
Overrides
Defined in
core/connection/Connection.ts:94
• new Connection(endpoint): Connection
Parameters
| Name | Type |
|---|---|
endpoint | string |
Returns
Deprecated
Use ConnectionConfig object instead
Overrides
EventEmitter<ConnectionEvents>.constructor
Defined in
core/connection/Connection.ts:98
Methods
getTransport
▸ getTransport(): Transport
Get the underlying transport instance
Returns
Defined in
core/connection/Connection.ts:154
getTransportType
▸ getTransportType(): string
Get the transport type
Returns
string
Defined in
core/connection/Connection.ts:161
getState
▸ getState(): ConnectionState
Get current connection state
Returns
Defined in
core/connection/Connection.ts:168
isConnected
▸ isConnected(): boolean
Check if connected
Returns
boolean
Defined in
core/connection/Connection.ts:175
connect
▸ connect(): Promise<void>
Connect using the underlying transport
Returns
Promise<void>
Defined in
core/connection/Connection.ts:182
disconnect
▸ disconnect(): void
Disconnect and stop reconnecting
Returns
void
Defined in
core/connection/Connection.ts:196
send
▸ send(data): Promise<void>
Send a message through the transport
Parameters
| Name | Type |
|---|---|
data | any |
Returns
Promise<void>
Defined in
core/connection/Connection.ts:207
setMaxReconnectAttempts
▸ setMaxReconnectAttempts(maxAttempts): void
Set maximum reconnection attempts
Parameters
| Name | Type |
|---|---|
maxAttempts | number |
Returns
void
Defined in
core/connection/Connection.ts:221
setReconnectDelays
▸ setReconnectDelays(baseDelay, maxDelay): void
Set reconnection delay parameters
Parameters
| Name | Type |
|---|---|
baseDelay | number |
maxDelay | number |
Returns
void
Defined in
core/connection/Connection.ts:228
on
▸ on<K>(event, handler): this
Register an event handler.
The handler will be called every time the event is emitted.
Type parameters
| Name | Type |
|---|---|
K | extends keyof ConnectionEvents |
Parameters
| Name | Type | Description |
|---|---|---|
event | K | The event name to listen for |
handler | EventHandler<ConnectionEvents[K]> | The function to call when the event is emitted |
Returns
this
this for method chaining
Example
agent.on("message", (event) => {
console.log("Received:", event.payload);
});Inherited from
Defined in
core/events/EventEmitter.ts:64
once
▸ once<K>(event, handler): this
Register a one-time event handler.
The handler will be called only once, then automatically removed.
Type parameters
| Name | Type |
|---|---|
K | extends keyof ConnectionEvents |
Parameters
| Name | Type | Description |
|---|---|---|
event | K | The event name to listen for |
handler | EventHandler<ConnectionEvents[K]> | The function to call when the event is emitted |
Returns
this
this for method chaining
Example
agent.once("connected", () => {
console.log("First connection established!");
});Inherited from
Defined in
core/events/EventEmitter.ts:88
off
▸ off<K>(event, handler?): this
Remove an event handler.
If no handler is specified, removes all handlers for the event.
Type parameters
| Name | Type |
|---|---|
K | extends keyof ConnectionEvents |
Parameters
| Name | Type | Description |
|---|---|---|
event | K | The event name |
handler? | EventHandler<ConnectionEvents[K]> | The specific handler to remove (optional) |
Returns
this
this for method chaining
Example
const handler = (event) => console.log(event);
agent.on("message", handler);
agent.off("message", handler);Example
agent.off("message");Inherited from
Defined in
core/events/EventEmitter.ts:117
removeAllListeners
▸ removeAllListeners<K>(event?): this
Remove all event handlers
Type parameters
| Name | Type |
|---|---|
K | extends keyof ConnectionEvents |
Parameters
| Name | Type |
|---|---|
event? | K |
Returns
this
Inherited from
EventEmitter.removeAllListeners
Defined in
core/events/EventEmitter.ts:180
listenerCount
▸ listenerCount<K>(event): number
Get the number of listeners for an event
Type parameters
| Name | Type |
|---|---|
K | extends keyof ConnectionEvents |
Parameters
| Name | Type |
|---|---|
event | K |
Returns
number