# WebSocketConnector
Source: https://docs.maelstrom-ai.co/dev/reference/client/classes/websocketconnector/
Documentation revision: 89a8f5e9d10cedcf0b30376d7846113f06f26120
SDK manifests in this build: @maelstrom-co/react@0.2.1, @maelstrom-co/client@0.2.1, @maelstrom-co/connectors@0.2.1, @maelstrom-co/protocol@0.2.1, @maelstrom-co/engine@0.2.1
These docs follow this source revision. Manifest versions do not prove release compatibility; verify the installed package exports before using an API.

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:137](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L137)

WebSocket connector for real-time bidirectional communication.

This connector is a thin wrapper around the WebSocket API. It:

-   Opens/closes WebSocket connections
-   Sends/receives messages with request/response correlation
-   Reports transport events via `subscribe()`

State management, retry logic, and circuit breaker patterns are handled by ConnectionManager which consumes the transport events.

## Example

```typescript
import { ConnectionManager, WebSocketConnector } from '@maelstrom-co/client';

const connector = new WebSocketConnector({
  url: 'wss://api.example.com/ws',
});

const manager = new ConnectionManager(store, connector);
await manager.connect(); // ConnectionManager handles state & retries
```

## Implements

-   [`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/)

## Constructors

### Constructor

> **new WebSocketConnector**(`options`): `WebSocketConnector`

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:213](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L213)

#### Parameters

##### options

[`WebSocketConnectorOptions`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/websocketconnectoroptions/)

#### Returns

`WebSocketConnector`

## Properties

### capabilities

> `readonly` **capabilities**: [`ConnectorCapabilities`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/connectorcapabilities/)

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:147](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L147)

Connector capabilities. Metadata for consumers — the ConnectionManager does not use these for flow control.

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`capabilities`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#capabilities)

## Methods

### addAssistantMessage()

> **addAssistantMessage**(`message`): `Promise`<`void`\>

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:498](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L498)

Sends an assistant message to the server. Fire-and-forget: sends the legacy `assistant_message` envelope, for which the server sends no result frame, and resolves after the socket accepts the send. Use [addAssistantMessageAcknowledged](https://docs.maelstrom-ai.co/dev/reference/client/classes/websocketconnector/#addassistantmessageacknowledged) when persistence must be confirmed before a dependent side effect. Throws if the WebSocket is not connected, construction rejects malformed content, or the socket cannot accept the send. Later Engine insertion failures are intentionally unobservable on this legacy path.

Translates the connector-facing [AssistantMessage](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/assistantmessage/) into the real `assistant_message` envelope via AssistantInjectionMessage so the frame goes through the same codec as every other send. The smart constructor stamps a `Date.now()` epoch timestamp — the previous bespoke frame used `performance.now()` (a monotonic clock offset from an arbitrary origin), which is not a wall-clock time. A compatible server sends no frame for insertion success or failure, so this method cannot observe either.

#### Parameters

##### message

[`AssistantMessage`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/assistantmessage/)

#### Returns

`Promise`<`void`\>

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`addAssistantMessage`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#addassistantmessage)

* * *

### addAssistantMessageAcknowledged()

> **addAssistantMessageAcknowledged**(`message`): `Promise`<[`AssistantMessageOutcome`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/assistantmessageoutcome/)\>

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:384](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L384)

Insert an assistant message and wait for the correlated [AssistantMessageOutcome](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/assistantmessageoutcome/) from `engine.assistant_message.result`.

Unlike [addAssistantMessage](https://docs.maelstrom-ai.co/dev/reference/client/classes/websocketconnector/#addassistantmessage), this sends the opt-in `client.assistant_message.acknowledged` envelope. A server that predates that envelope may reject it with a correlated `engine.error` or stay silent; those paths reject promptly or at the bounded timeout rather than being mistaken for successful persistence.

Correlation uses `message.id` as `requestId` and times out after the connector `timeout` (default 30s).

#### Parameters

##### message

[`AssistantMessage`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/assistantmessage/)

#### Returns

`Promise`<[`AssistantMessageOutcome`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/assistantmessageoutcome/)\>

#### Throws

If the WebSocket is disconnected or the session is not yet established, or if construction rejects malformed message content.

#### Throws

If `WebSocket.send` fails while enqueueing the frame; the pending correlator is cleared before the returned promise rejects.

#### Throws

If the engine reports a correlated `engine.error`.

#### Throws

If the socket closes before the result arrives, or if the acknowledgement times out.

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`addAssistantMessageAcknowledged`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#addassistantmessageacknowledged)

* * *

### connect()

> **connect**(`sessionId`): `Promise`<`void`\>

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:253](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L253)

Establishes WebSocket connection. Throws on failure — ConnectionManager will handle retries.

Idempotent only for the SAME `sessionId`: a second call for that session while a socket is already open or connecting is a no-op (open) or joins the in-flight attempt (connecting) instead of constructing another `WebSocket`, since the underlying socket assignment below does not itself guard against overwriting a live one. A call with a DIFFERENT `sessionId` is never idempotent — it always reassigns `#sessionId` and dials a fresh socket, even over one that is open or connecting, because silently keeping the old session would leave `#sessionId` (embedded in outbound envelopes such as `assistant_message`) pointing at a session this call was explicitly asked to move away from.

#### Parameters

##### sessionId

[`SessionId`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/sessionid/)

#### Returns

`Promise`<`void`\>

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`connect`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#connect)

* * *

### disconnect()

> **disconnect**(): `Promise`<`void`\>

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:344](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L344)

Closes the WebSocket connection and rejects all pending requests.

#### Returns

`Promise`<`void`\>

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`disconnect`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#disconnect)

* * *

### getHistory()

> **getHistory**(`sessionId`): `Promise`<readonly `SessionHistoryEntry`\[\]>

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:526](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L526)

Sends a `client.history.get` request and waits for the correlated `engine.history.response`. Correlation goes through `#historyCorrelator` (typed for `SessionHistoryEntry[]`) so the response can’t be misrouted to `process()`’s `MaelstromResponse` correlator even if the freshly generated request id happens to collide with an in-flight chart request.

#### Parameters

##### sessionId

[`SessionId`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/sessionid/)

#### Returns

`Promise`<readonly `SessionHistoryEntry`\[\]>

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`getHistory`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#gethistory)

* * *

### onEvent()

> **onEvent**(`handler`): [`Unsubscribe`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/unsubscribe/)

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:450](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L450)

Subscribe to AgentEvents the engine sends over this socket. Only events from the currently connected socket are delivered, so events from a replaced or disconnected socket are dropped. A throwing handler is logged and does not affect other handlers.

The agent loop is experimental in 0.3 and may change in any minor release.

> [!WARNING]
> Experimental
> 
> This API should not be used in production and may be trimmed from a public release.

#### Parameters

##### handler

(`event`) => `void`

#### Returns

[`Unsubscribe`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/unsubscribe/)

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`onEvent`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#onevent)

* * *

### process()

> **process**(`message`): `Promise`<`MaelstromResponse`\>

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:470](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L470)

Sends a chart\_request envelope and waits for the correlated response.

#### Parameters

##### message

`ChartRequestMessage`

#### Returns

`Promise`<`MaelstromResponse`\>

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`process`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#process)

* * *

### sendInput()

> **sendInput**(`input`): `Promise`<`void`\>

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:427](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L427)

Send one AgentInput as a `client.agent.input` frame for the connected session. Resolves once the socket accepted the frame and does not wait for any AgentEvent.

> [!WARNING]
> Experimental
> 
> This API should not be used in production and may be trimmed from a public release.

#### Parameters

##### input

`AgentInput`

#### Returns

`Promise`<`void`\>

#### Throws

With kind `transport` when the socket is not open or no session is established yet.

#### Throws

If `WebSocket.send` fails.

The agent loop is experimental in 0.3 and may change in any minor release.

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`sendInput`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#sendinput)

* * *

### setDiagnosticContent()

> **setDiagnosticContent**(`enabled`): `void`

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:180](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L180)

Enables server content forwarding only for an explicit local capture.

#### Parameters

##### enabled

`boolean`

#### Returns

`void`

* * *

### subscribe()

> **subscribe**(`handler`): [`Unsubscribe`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/unsubscribe/)

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:460](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L460)

Subscribe to transport-level events (disconnected, error).

#### Parameters

##### handler

(`event`) => `void`

#### Returns

[`Unsubscribe`](https://docs.maelstrom-ai.co/dev/reference/client/type-aliases/unsubscribe/)

#### Implementation of

[`AcknowledgedAssistantConnector`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/).[`subscribe`](https://docs.maelstrom-ai.co/dev/reference/client/interfaces/acknowledgedassistantconnector/#subscribe)

* * *

### subscribeDiagnostics()

> **subscribeDiagnostics**(`listener`): () => `void`

Defined in: [packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts:171](https://github.com/maelstrom-co/maelstrom/blob/89a8f5e9d10cedcf0b30376d7846113f06f26120/packages/client/src/lib/connection-manager/connectors/web-socket-connector.ts#L171)

Observes optional server traces and enables forwarding only while subscribed.

#### Parameters

##### listener

(`event`) => `void`

#### Returns

() => `void`
