Read raw transport messages
Observe every message a session receives, on any transport, with the typed rawMessage event, including MCP tool lifecycle frames and unknown types.
Observe the messages a Convai session receives, as decoded from the wire and before the SDK interprets them. Use the rawMessage event to log traffic, to read MCP tool lifecycle frames, or to handle message types the SDK does not turn into its own events. It works the same on LiveKit, WebSocket and SSE.
Prerequisites
The Convai Web SDK installed, with a client that connects. See Web SDK quickstart.
Subscribe to raw messages
Call client.on('rawMessage', listener). It returns an unsubscribe function. The React hook exposes the same listener through its on and off methods.
import { useEffect } from 'react';
import { useConvaiClient } from '@convai/web-sdk/react';
import { ConvaiWidget } from '@convai/web-widget/react';
const config = {
apiKey: 'YOUR_API_KEY',
characterId: 'YOUR_CHARACTER_ID',
startWithAudioOn: false,
};
export default function Chat() {
const client = useConvaiClient(config);
useEffect(() => client.on('rawMessage', (event) => {
console.log(event.transport, event.message);
if (event.transport === 'sse') console.log(event.request.logicalTurnIds);
}), [client.on]);
return <ConvaiWidget convaiClient={client} />;
}Return the unsubscribe function from the effect, as above, so unmounts and rerenders remove the listener.
You can also remove the listener with client.off('rawMessage', listener), passing the same function reference.
Subscribing does not connect or send anything. The widget, or your own call to connect(), controls the connection. The listener covers inbound messages only; it does not publish events or observe outgoing traffic.
Read the event payload
Every event carries message, byteLength and transport. The remaining field depends on the transport, so check event.transport first and TypeScript narrows the type for you.
Transport
message
Extra field
'livekit'
The decoded JSON envelope, including nested server-message data. Non-JSON packets are not emitted.
participant: the sender supplied by the room. It can be absent.
'websocket'
The decoded JSON envelope, including nested server-message data. Non-JSON packets are not emitted.
None
'sse'
{ type: eventName, data: decodedData }. Data that is not JSON stays a string.
request: the originating request's logicalTurnIds and messageTypes.
messageisunknown. Validate its shape before you use it.messageand the SSE request metadata are detached from the SDK's own processing. Treat them as read-only.byteLengthis the incoming payload size. For SSE it counts the joined data fields, without event framing.On SSE, session, done, error and unknown frames are included. SSE comments and frames without data are not.
SSE reports individual text frames before the SDK batches them into chat messages.
Check where a message came from
On LiveKit, validate the sender with event.participant against the current room. It is the original participant object, not a copy.
On SSE, use event.request.logicalTurnIds to match a frame to the user turn that caused it. Control-only requests can have an empty list. When a request is interrupted, its remaining frames are not emitted.
Read MCP tool lifecycle frames
Messages about tool calls, including MCP tools, arrive on rawMessage like any other frame, along with event types the SDK does not interpret. Read the type field on message to pick out the ones you need, for example llm-function-call-in-progress and llm-function-call-stopped, or a server-message whose data.type is mcp-tool-heartbeat.
When to use rawMessage
Use the SDK's normal events, listed in the Web SDK event reference, for anything they already cover. Use rawMessage for what they do not: debugging and logging traffic, tool and MCP lifecycle frames, server message types the SDK does not interpret, and per-request correlation on SSE.
An exception thrown in your listener does not stop the SDK from handling the message normally.
API reference
client.on('rawMessage', listener)
(listener: (event: RawMessageEvent) => void) => () => void
Subscribes to incoming messages. Returns an unsubscribe function.
client.off('rawMessage', listener)
void
Removes the same listener by reference.
RawMessageEvent
union type
The event payload. Narrow it by transport.
event.message
unknown
Decoded wire message, before SDK interpretation. Validate its shape before use.
event.byteLength
number
Incoming payload size. For SSE, the joined data fields without event framing.
event.transport
'livekit' | 'websocket' | 'sse'
Transport that delivered the message.
event.participant
unknown
LiveKit only. Sender supplied by the room; can be absent.
event.request
SSERequestContext
SSE only. The originating request's logicalTurnIds and messageTypes.
SSERequestContext
{ transport: 'sse'; logicalTurnIds: string[]; messageTypes: string[] }
Identity of the SSE request that produced the event.
Last updated
Was this helpful?