Class: WebSocketClient

WebSocketClient(parameters)

WebSocket client for Kristen Data Comms (KDC-JS). Provides communication with a KDC WebSocket server, supporting asynchronous Agent transmission, synchronous request/reply operations, and reception of Agents and Replies. Supports automatic connection startup through the autoStart parameter. When enabled, the client begins connecting during construction. When disabled, connect() must be called explicitly. Repeated calls to connect() reuse an established or in-progress connection without creating an additional WebSocket. Send operations wait for an in-progress connection to complete. Concurrent asynchronous sends are serialized to preserve send order. Synchronous requests use a separate mutex, allowing only one request to await a Reply at a time. Incoming messages are deserialized into registered Agent classes and delivered to registered event handlers.

Constructor

new WebSocketClient(parameters)

Creates a WebSocket client using the supplied parameters. If autoStart is enabled, connection establishment begins automatically during construction. The constructor does not wait for the connection to complete. If autoStart is disabled, connect() must be called explicitly before sending Agents. Automatic connection failures are logged.
Parameters:
Name Type Description
parameters WebSocketClientParameters WebSocket client configuration.
Throws:
If parameters is not a WebSocketClientParameters instance.
Type
Error

Classes

WebSocketClient

Methods

(async) close() → {Promise.<void>}

Closes the WebSocket connection. Rejects any pending synchronous request and initiates a normal WebSocket closure. If no connection exists, returns immediately.
Returns:
Resolves when the connection has closed.
Type
Promise.<void>

connect() → {Promise.<WebSocketClient>}

Establishes a connection to the configured WebSocket server. This method is idempotent: - If connected, resolves with this client. - If connecting, returns the existing connection Promise. - If closing, rejects the connection request. - If disconnected, initiates a new connection. Multiple calls do not create additional connections while an existing connection is open or being established. Registered event handlers are notified when a new connection is established.
Throws:
If the connection is closing or establishment fails.
Type
Error
Returns:
Resolves with this client when connected.
Type
Promise.<WebSocketClient>

isConnected() → {boolean}

Determines whether the WebSocket connection is open. Returns false while connecting, closing, or disconnected.
Returns:
True if the WebSocket is open; otherwise false.
Type
boolean

(async) sendAsync(agent) → {Promise.<void>}

Sends an Agent asynchronously. If a connection is being established, waits for it to complete before transmitting the Agent. This method does not initiate a connection when disconnected. Marks the Agent as asynchronous, assigns the SEND_ASYNC latency context, serializes the Agent, and submits its bytes to the WebSocket. Concurrent asynchronous sends are serialized using an internal mutex to preserve send order. Notifies registered event handlers before sending.
Parameters:
Name Type Description
agent Agent Agent to transmit.
Throws:
If the Agent is invalid or the WebSocket is not open.
Type
Error
Returns:
Resolves after the Agent has been submitted to the WebSocket for transmission.
Type
Promise.<void>

(async) sendSync(agent) → {Promise.<Reply>}

Sends an Agent synchronously and waits for its Reply. If a connection is being established, waits for it to complete before transmitting the Agent. This method does not initiate a connection when disconnected. The synchronous-send mutex remains held until the matching Reply arrives, the request times out, or an error occurs. Marks the Agent as synchronous, assigns the SEND_SYNC latency context, serializes and transmits the Agent, and waits for a Reply with a matching UUID. Only one synchronous request is active at a time. Additional requests wait for the internal mutex.
Parameters:
Name Type Description
agent Agent Agent to transmit.
Throws:
If the Agent is invalid, the WebSocket is not open, the send operation fails, the connection closes, or the Reply timeout expires.
Type
Error
Returns:
Reply matching the transmitted Agent UUID.
Type
Promise.<Reply>