Skip to content

Class: WebSocketConnection

@kortexya/reasoninglayer


@kortexya/reasoninglayer / WebSocketConnection

Class: WebSocketConnection

Defined in: src/websocket.ts:18

An active WebSocket connection with auto-reconnect.

Remarks

Uses the native WebSocket API (Node 18+, browsers, Deno, Bun). Automatically reconnects with exponential backoff + jitter (1s -> 2s -> 4s -> … -> max 30s), up to 10 attempts.

Accessors

connected

Get Signature

get connected(): boolean

Defined in: src/websocket.ts:53

Whether the WebSocket is currently connected.

Returns

boolean

Methods

close()

close(): void

Defined in: src/websocket.ts:100

Close the WebSocket connection.

Returns

void

Remarks

Once closed, the connection will not attempt to reconnect.


onClose()

onClose(handler): void

Defined in: src/websocket.ts:81

Register a handler for connection close events.

Parameters

handler

() => void

Called when the connection closes.

Returns

void


onError()

onError(handler): void

Defined in: src/websocket.ts:90

Register a handler for connection errors.

Parameters

handler

(error) => void

Called with the error.

Returns

void


onMessage()

onMessage<T>(handler): void

Defined in: src/websocket.ts:62

Register a handler for incoming messages.

Type Parameters

T

T = unknown

Parameters

handler

MessageHandler<T>

Called with the parsed JSON message data.

Returns

void


onReconnect()

onReconnect(handler): void

Defined in: src/websocket.ts:72

Register a handler for reconnection events.

Parameters

handler

ReconnectHandler

Called with the reconnection attempt number.

Returns

void