bodhi-realtime-agent / ClientTransport
Class: ClientTransport
Defined in: src/transport/client-transport.ts:83
WebSocket server that bridges a client audio app to the framework.
Multiplexes two message types on the same WebSocket connection:
- Binary frames: Raw PCM audio (forwarded via
onAudioFromClientor buffered during transfers). - Text frames: JSON messages for GUI events (
onJsonFromClient).
Buffering mode (startBuffering/stopBuffering) only affects binary audio frames. Text frames are always delivered immediately.
Pre-client interception, recognized from the upgrade request URL query BEFORE any connection is attached as the client:
?probe=1— health probe: the upgrade completes, one JSON text frame fromoptions.probeStateis sent (if provided), then the socket closes with 1000. Probe sockets never touch the attached client, never fire connect/disconnect callbacks, and are excluded from all client accounting.?takeover=1— user-confirmed takeover: an incumbent real client is closed with 4410superseded-by-takeover, then the challenger attaches as real.?verify=1— low-priority verification role: rejected with 4409client-busywhile a real client is attached; preempted with 4411verifier-preemptedwhen a real client arrives. FiresonVerifierConnected/onVerifierDisconnectedonly — never the real-client callbacks — and never counts as attached.
Any other connection is a real client. A second real client is rejected with 4409 client-busy while one is attached.
Constructors
Constructor
new ClientTransport(
port,callbacks,host?,listenTimeoutMs?,options?):ClientTransport
Defined in: src/transport/client-transport.ts:102
Parameters
port
number
callbacks
host?
string = '0.0.0.0'
listenTimeoutMs?
number = 10_000
options?
Returns
ClientTransport
Properties
supportsPlaybackStateProtocol
readonlysupportsPlaybackStateProtocol:true=true
Defined in: src/transport/client-transport.ts:100
Audio (binary) and JSON (text) share the one WebSocket and are sent synchronously in call order, so this transport supports the playback-state protocol.
Accessors
attachedRole
Get Signature
get attachedRole():
ClientConnectionRole|null
Defined in: src/transport/client-transport.ts:399
Role of the currently attached connection, or null when none.
Returns
ClientConnectionRole | null
buffering
Get Signature
get buffering():
boolean
Defined in: src/transport/client-transport.ts:403
Returns
boolean
isClientConnected
Get Signature
get isClientConnected():
boolean
Defined in: src/transport/client-transport.ts:389
True when a REAL client is attached and open. Verifier and probe connections never count (client accounting is real-clients-only).
Returns
boolean
isVerifierConnected
Get Signature
get isVerifierConnected():
boolean
Defined in: src/transport/client-transport.ts:394
True when a verification-role (?verify=1) connection is attached and open.
Returns
boolean
Methods
closeClient()
closeClient(
code?,reason?):boolean
Defined in: src/transport/client-transport.ts:314
Close the attached socket with the given close code and reason. The listener keeps accepting connections (stop() is the listener teardown), and the socket's close runs the usual disconnect handling. Returns false when no connection holds the client slot.
Parameters
code?
number
reason?
string
Returns
boolean
discardBuffered()
discardBuffered():
void
Defined in: src/transport/client-transport.ts:356
Leave buffering mode and drop the buffered microphone frames (and their capture tags) without returning them, so none reaches the model.
Returns
void
getSocketHealth()
getSocketHealth():
ClientSocketHealth|null
Defined in: src/transport/client-transport.ts:305
readyState and bufferedAmount of the attached socket, or null when no connection holds the client slot.
Returns
ClientSocketHealth | null
sendAudioToClient()
sendAudioToClient(
data):void
Defined in: src/transport/client-transport.ts:321
Send raw PCM audio to the client as a binary frame.
Parameters
data
Buffer
Returns
void
sendJsonAfterAudio()
sendJsonAfterAudio(
message):void
Defined in: src/transport/client-transport.ts:339
Playback-state protocol: deliver JSON in order after the turn's audio. Binary and text frames go out on the one socket synchronously in call order, so this is sendJsonToClient issued after the audio sends.
Parameters
message
AnyServerToClientMessage
Returns
void
sendJsonToClient()
sendJsonToClient<
T>(message):void
Defined in: src/transport/client-transport.ts:330
Send a JSON message to the client as a text frame. Accepts core frames, registered extensions, or an application HostClientFrame whose type is not a core frame type; the frame is serialized verbatim.
Type Parameters
T
T extends string
Parameters
message
AnyServerToClientMessage | HostClientFrame<T>
Returns
void
start()
start():
Promise<void>
Defined in: src/transport/client-transport.ts:110
Returns
Promise<void>
startBuffering()
startBuffering():
void
Defined in: src/transport/client-transport.ts:343
Returns
void
stop()
stop():
Promise<void>
Defined in: src/transport/client-transport.ts:284
Returns
Promise<void>
stopBuffering()
stopBuffering():
Buffer<ArrayBufferLike>[]
Defined in: src/transport/client-transport.ts:348
Returns
Buffer<ArrayBufferLike>[]