Callbacks and events
The Pipecat JavaScript client listens for messages and events from the bot via the transport layer. This allows you to respond to changes in state, errors, and other events. The client implements the RTVI standard for these communications.
Event Handling Options
You can handle events in two ways:
1. Callbacks
Define handlers in the client constructor:
2. Event Listeners
Add handlers using the event emitter pattern:
Events and callbacks provide the same functionality. Choose the pattern that best fits your application’s architecture.
Callbacks
Messages and errors
If the client receives an unknown message type from the transport (see messages and events), it can be handled here.
Response error when an action fails or an unknown message type is sent from the client.
Error signalled by the bot. This could be due to a malformed config update or an unknown action dispatch or the ability to complete the requested action.
State and connectivity
Provides a TransportState
string representing the connectivity state of the
local client. See transports for state explanation.
Local user successfully established a connection to the transport.
Local user disconnected from the transport, either intentionally by calling
rtviClient.disconnect()
or due to an error.
Bot connected to the transport and is configuring. Note: bot connectivity does
not infer that its pipeline is yet ready to run. Please use onBotReady
instead.
A non-bot participant joined the session.
A non-bot participant left the session. Note: excluded local participant.
The bot has been instantiated, its pipeline is configured, and it is receiving
user media and interactions. This method is passed a BotReadyData
object,
which contains a config RTVIClientConfigOption[]
array and the RTVI
version
number. It is recommended to hydrate your application with the
passed config in case the bot alters any configuration based on its
implementation. Since the bot is remote and may be using a different version
of RTVI than the client, you can use the passed version
string to check for
compatibility.
Bot disconnected from the transport. This may occur due to session expiry, a pipeline error or because the local participant left the session.
Configuration
Sent when the bots configuration changes. This is most likely in response to a
user updateConfig
action, but can occur within a bots pipeline in response
to actions or messages.
A list of available configuration options for each service. Sent in response to a user describeConfig()
call.
Actions
Response to describeActions()
call. Lists available actions for the bot and
their associated arguments and types.
Media and devices
Lists available local media microphone devices. Triggered when a new device
becomes available or in response to rtviClient.initDevices()
.
Lists available local media camera devices. Triggered when a new device
becomes available or in response to rtviClient.initDevices()
.
User selected a new microphone as their selected/active device.
User selected a new camera as their selected/active device.
Media track from a local or remote participant was started and playable. Can be either an audio or video track.
Media track from a local or remote participant was stopped and no longer playable.
Audio and Voice Activity
Local audio gain level (0 to 1).
Remote audio gain level (0 to 1). Note: if more than one participant is
connected to the transport, the participant
property details the associated
peer.
The bot started speaking/sending speech audio.
The bot stopped speaking/sending speech audio.
The local user started speaking. This method is more reliable than using audio gain and is the result of the bot’s VAD (voice activity detection) model. This provides a more accurate result in noisy environments.
The local user stopped speaking, indicated by the VAD model.
Transcription
Transcribed local user input (both partial and final).
Callback receives a TranscriptData
object:
Finalized bot output text generated by the LLM. Sentence aggregated.
Streamed LLM token response text generated by the LLM service. Please note that this event is not dispatched when in a connected state. This callback is intended to be used for offline, single-turn actions, where not TTS service is connected.
When connected:
-
If you want to align words as they are spoken by the bot, use
onBotTtsText
instead. -
If you want the full text of the LLM response, use
onBotTranscript
.
If your TTS service supports streamed responses over sockets, the text parameter contains the words from TTS service as they are spoken If you are using a HTTP based TTS service, the text parameter will contain the full text of the TTS response.
Other
Pipeline data provided by Pipecat. Please see Pipecat documentation for more information.
A message or messages item was saved to the storage / database. Assumed you have a storage context processor as part of your bot pipeline. Data references the saved message.
Events
Each callback described above has a corresponding event that can be listened for using the .on()
method. This allows you to handle the same functionality using either callbacks or event listeners, depending on your preferred architecture.
Here’s the complete reference mapping events to their corresponding callbacks:
Connection Events
Event Name | Callback Name | Data Type |
---|---|---|
connected | onConnected | - |
disconnected | onDisconnected | - |
transportStateChanged | onTransportStateChanged | TransportState |
botConnected | onBotConnected | - |
botReady | onBotReady | BotReadyData |
botDisconnected | onBotDisconnected | Participant |
Participant Events
Event Name | Callback Name | Data Type |
---|---|---|
participantConnected | onParticipantJoined | Participant |
participantLeft | onParticipantLeft | Participant |
Media Events
Event Name | Callback Name | Data Type |
---|---|---|
trackStarted | onTrackStarted | MediaStreamTrack, Participant |
trackStopped | onTrackStopped | MediaStreamTrack, Participant |
availableMicsUpdated | onAvailableMicsUpdated | MediaDeviceInfo[] |
availableCamsUpdated | onAvailableCamsUpdated | MediaDeviceInfo[] |
micUpdated | onMicUpdated | MediaDeviceInfo |
camUpdated | onCamUpdated | MediaDeviceInfo |
Audio Activity Events
Event Name | Callback Name | Data Type |
---|---|---|
localAudioLevel | onLocalAudioLevel | number |
remoteAudioLevel | onRemoteAudioLevel | number, Participant |
botStartedSpeaking | onBotStartedSpeaking | - |
botStoppedSpeaking | onBotStoppedSpeaking | - |
userStartedSpeaking | onUserStartedSpeaking | - |
userStoppedSpeaking | onUserStoppedSpeaking | - |
Text and Transcription Events
Event Name | Callback Name | Data Type |
---|---|---|
userTranscript | onUserTranscript | TranscriptData |
botTranscript | onBotTranscript | BotLLMTextData |
botLlmText | onBotLLMText | BotLLMTextData |
botTtsText | onBotTtsText | BotTTSTextData |
Service State Events
Event Name | Callback Name | Data Type |
---|---|---|
botLlmStarted | onBotLlmStarted | - |
botLlmStopped | onBotLlmStopped | - |
botTtsStarted | onBotTtsStarted | - |
botTtsStopped | onBotTtsStopped | - |
Configuration Events
Event Name | Callback Name | Data Type |
---|---|---|
config | onConfigUpdated | RTVIClientConfigOption[] |
configDescribe | onConfigDescribe | unknown |
actionsAvailable | onActionsAvailable | unknown |
Other Events
Event Name | Callback Name | Data Type |
---|---|---|
metrics | onMetrics | PipecatMetricsData |
storageItemStored | onStorageItemStored | StorageItemStoredData |