The RTVIClient uses an event-driven model to communicate session state, transcription, and media changes. You can subscribe to events using the .on(event, callback) method.
Commonly used RTVIEvent types include:
TransportStateChanged: Emits the current state of the transport.Connected / Disconnected: Emits when the user connection changes.BotConnected / BotDisconnected: Emits when the bot connection changes.BotReady: Emitted when the bot is ready for interaction.TrackStarted: Emitted when a new media track (audio/video) is received. The callback provides the MediaStreamTrack and the Participant who owns it.UserStartedSpeaking / UserStoppedSpeaking: Emits when user voice activity is detected.BotStartedSpeaking / BotStoppedSpeaking: Emits when the bot is generating audio.UserTranscript: Emits transcription data for the user. The callback receives TranscriptData which includes a text string and a final boolean.BotTranscript: Emits text data from the bot. The callback receives BotLLMTextData.Error / MessageError: Emits error information as an RTVIMessage.Metrics: Emits performance metrics, such as ttfb (Time To First Byte).
import { RTVIClient, RTVIEvent } from "@pipecat-ai/client-js";
export async function setupEventHandlers(rtviClient: RTVIClient) {
rtviClient.on(RTVIEvent.Connected, () => {
console.log("-- user connected --");
});
rtviClient.on(RTVIEvent.UserTranscript, (transcript) => {
if (transcript.final) {
console.log('final:', transcript.text);
} else {
console.log('interim:', transcript.text);
}
});
rtviClient.on(RTVIEvent.BotTranscript, (data) => {
console.log('bot text:', data.text);
});
rtviClient.on(RTVIEvent.TrackStarted, (track, participant) => {
if (!participant.local) {
// Handle remote track
}
});
}