Realtime protocol
How the conversation WebSocket works. Reference material — there is no public entry point to it.
This page is a description of the wire format, not a set of instructions. It documents the handshake, the message tags, the audio format and the failure modes, because the protocol is stable and knowing it helps when you are debugging a session, reasoning about latency, or judging what an embed can and cannot do. It is not a path you can build against today.
What a realtime client does
Whatever speaks this protocol — the dashboard, the embed page — carries four responsibilities:
- Capture microphone audio and stream it up continuously.
- Receive interleaved video frames and audio chunks.
- Play the audio while rendering frames in step with it.
- Track conversation state so the interface can show what the avatar is doing.
The handshake
The URL comes from the dashboard's own POST /conversations, which returns a
websocket_url with a session_token query parameter already attached. That token is the
credential; the engine verifies it during the handshake. It is passed through unmodified —
rebuilding the URL or stripping the query string fails the handshake.
const socket = new WebSocket(websocketUrl);
socket.binaryType = "arraybuffer";
The dashboard's token is valid for 10 minutes, so the dashboard opens the connection
promptly after creating the conversation. The embed carries the token from
conversation_url, which is valid for 60 minutes. If the token is missing, malformed,
or expired, the engine refuses the connection. It does so before accepting it, so a browser
never sees the engine's close code (4001): the handshake fails, onerror fires, and
onclose reports 1006 without onopen ever having fired. A refused token is terminal for
that URL: the fix is a new conversation, not a retry.
Wire protocol
Every binary message starts with a one-byte type tag, followed by its payload.
| Tag | Name | Payload |
|---|---|---|
0x01 | Video | JPEG frame. Speaking frames are prefixed with a 4-byte little-endian index; idle frames with an 8-byte timestamp |
0x02 | Audio | A complete encoded audio blob (legacy path) |
0x03 | Text | UTF-8 JSON |
0x04 | Audio chunk | 4-byte LE chunk index, 1-byte flags, then PCM16 at 16 kHz |
socket.onmessage = (event) => {
if (typeof event.data === "string") return; // not used; frames are binary
const view = new Uint8Array(event.data);
const tag = view[0];
const payload = view.subarray(1);
switch (tag) {
case 0x01: renderVideoFrame(payload); break;
case 0x03: handleTextMessage(JSON.parse(new TextDecoder().decode(payload))); break;
case 0x04: enqueueAudioChunk(payload); break;
}
};
Audio chunks
For tag 0x04, the first four bytes are the chunk index, the fifth is a flags byte, and the
remainder is PCM16 audio at 16 kHz.
function enqueueAudioChunk(payload) {
const flags = payload[4];
const isLastChunkOfTurn = (flags & 0x01) !== 0;
const pcm = payload.slice(5);
audioQueue.push(pcm);
if (isLastChunkOfTurn) markTurnComplete();
}
Bit 0x01 of the flags byte marks the final chunk of a turn, which is how a client knows
the avatar has finished speaking without waiting on a timeout.
Text messages
Tag 0x03 carries JSON with a type and content. New message types appear over time, so
a client parses defensively and discards anything malformed rather than tearing down the
connection.
Microphone audio
Capture is 16 kHz mono, resampled if the input device runs at a different rate, sent as fixed-size binary frames. The reference client uses 3200-sample chunks — 200 ms at 16 kHz, small enough to keep latency low, large enough to avoid flooding the socket.
Resampling and chunking happen in an AudioWorklet rather than on the main thread. Anything
that blocks the main thread shows up as audible stutter.
Conversation state
The avatar's state is surfaced in the interface so the user knows whether it is their turn:
connecting → ready → listening ⇄ thinking ⇄ speaking
Showing thinking matters more than it sounds. Without it, the pause between a user
finishing their sentence and the avatar starting to speak reads as a broken connection.
Audio and video sync
Video frames and audio chunks arrive as separate messages, and playing each as it lands produces visible lip-sync drift. Rendering is driven from audio playback position rather than from frame arrival: the decoded audio sits in a queue, and frame selection follows how much of it has actually played.
Speaking frames carry a sequence index for exactly this reason. Idle frames carry a timestamp instead, since they are not tied to speech.
Adaptive video quality
The engine chooses the size of the video it sends from how well the connection keeps up, and changes it during the session. Only the video changes; the audio does not.
The sizes are steps down from the avatar's own footage, by height and in the footage's own aspect ratio. The top step is the footage itself, capped at 1280 px on the long edge, and JPEG quality is 75 at every step:
| Footage | Steps |
|---|---|
| 1080×1920 | 720p → 540p → 480p → 360p |
| 464×832 | 464p → 360p |
Footage smaller than a step skips it, so a small avatar has fewer steps.
- Start. A session starts one step below the top and climbs to the top once the connection has stayed clean for a while.
- Down. When frames back up between the engine and the viewer, the engine drops quickly to a size the measured connection can carry. A pause the connection did not cause, such as the engine itself being slow to produce frames, does not lower the size.
- Up. Quality climbs one step at a time after about 15 seconds without problems, or 20 seconds while the avatar is speaking. If a step fails, the engine waits a minute before trying it again, doubling the wait each time it fails, up to 10 minutes.
- Below the bottom step. On a connection too slow even for the smallest size, the engine sends fewer frames, down to 8 per second, rather than lowering the quality further.
What a client receives
The engine sends a wire_tier text message when the session starts and again on every
change:
{ "tier": 1, "name": "540p", "width": 540, "height": 960, "reason": "initial" }
tier is the step, with 0 the top; name, width and height are the size of the
frames that follow. reason is a short label for display and logs. Its values are not
fixed, so a client shows it rather than branching on it. The message carries other fields
as well, which a client can ignore.
The message is informational. Each JPEG carries its own dimensions, and frames change size mid-session, so a client draws each frame at the size it decodes to and scales it to fit the view. Fixing the canvas from the first frame stretches or crops every frame after a change.
What a client sends
The dashboard and the embed report what they receive. They announce themselves at the start of the session, then send link reports while it runs. The engine uses those reports to tell a slow connection apart from a busy browser. A client that sends none still gets adaptive video, judged from the engine's own measurement of how quickly its sends drain. The format of those reports is not part of this description.
Failure modes
| Situation | What it looks like | How it is handled |
|---|---|---|
| Refused handshake — expired or invalid token, engine at capacity, avatar files missing | onclose with 1006 before onopen fired. The engine's own codes (4001, 4503, 4404) are sent before it accepts and do not reach a browser | Create a new conversation. Each engine pod runs a fixed number of concurrent sessions — 4 by default — so a new conversation can also land on a pod with room |
| Failure after the connection opened | onclose with 4503 (pod_at_capacity, the pod filled as it accepted), 4404 (avatar files missing) or 4500 (avatar or engine failed to load) | Show an error; retry once with a new conversation |
| Network drop mid-session | onclose with any other code except 1000, after onopen | Reconnect with the same URL if the token is still valid; otherwise create a new conversation |
| Microphone permission denied | getUserMedia rejects | Prompt before connecting, so the failure comes early rather than mid-session |
The socket is opened only after microphone access is granted. Opening it first spends part of the token's lifetime sitting on a permission dialog.
Because a refused handshake carries no code, a browser cannot tell an expired token from
a full pod. What it can tell is whether onopen fired. The reference client in the
dashboard and the embed uses exactly that: a close before onopen is a fatal error, and a
close after it reconnects automatically, up to three times, for any code other than 1000.
What a correct client gets right
The seven things that go wrong
- Microphone permission is requested before the socket opens
- A close before
onopenis a refused handshake: create a new conversation rather than retrying the URL. Abnormal closes afteronopenreconnect a bounded number of times - Audio is captured at 16 kHz mono, resampled in a worklet
- Frame presentation is driven by audio playback position, not arrival time
- Each frame is drawn at its own size, which changes when the video quality does
thinkingis visible in the UI- Unknown text message types are ignored, not fatal