Comma Agents
@comma-agents/tuiHooks

useWebSocket

Generic WebSocket connection hook with reconnection, pending message queue, and status tracking.

The lowest-level networking hook. Manages a single WebSocket connection with reconnection on URL change, message buffering during connection, and status reporting. Used internally by DaemonContextProvider -- most consumers never call this directly.

import { useWebSocket } from "@comma-agents/tui";

const { status, send, close } = useWebSocket({
  url: "ws://localhost:7422/ws",
  onMessage: (data) => {
    const parsed = JSON.parse(data);
  },
  onError: (error) => console.error(error),
});

Configuration

OptionTypeDescription
urlstringWebSocket server URL
onMessage(data: string) => voidCalled when a text frame arrives
onStatus(status: WebSocketStatus) => voidOptional status change callback
onError(error: string) => voidOptional error callback

Return Value

FieldTypeDescription
statusWebSocketStatusCurrent connection status
send(data: string) => booleanSend a raw text frame; returns false if closed
close() => voidClose the WebSocket and clear pending messages

WebSocketStatus

ValueMeaning
"disconnected"No active connection
"connecting"WebSocket is handshaking
"connected"Open and ready for messages
"error"A connection error occurred

Pending Message Queue

Messages sent via send() while the socket is "connecting" are queued and flushed in order once the open event fires. send() returns false only when the socket is neither open nor connecting.

Reconnection

The hook reacts to URL changes by closing the current connection and opening a new one. Callbacks (onMessage, onStatus, onError) are stored in refs, so the effect does not re-run when callback identities change.

On this page