Adding Event Listeners
Register event listeners after creating the Anam client and before starting the session withstream() or streamToVideoElement().
Use the addListener method before the start call:
Available Events
Type Definitions
Message
MessageStreamEvent
DirectorNoteCueAppliedEvent
ToolCallStartedPayload
ToolCallCompletedPayload
ToolCallFailedPayload
ToolCallHandler
ClientToolEvent (Deprecated)
ConnectionClosedCode
ReasoningMessage
ReasoningStreamEvent
Example Usage
Loading States
Use connection events to manage loading states:Connection Closed Handling
Handle connection closures with reason codes:Microphone Permission Flow
Track microphone permission state:Speech detection
Know when the user starts and stops speaking, before any transcript is available:MESSAGE_STREAM_EVENT_RECEIVED. The correlationId ties the speech start/end pair to the eventual transcript.
If USER_SPEECH_ENDED doesn’t arrive within ~10 seconds of a USER_SPEECH_STARTED, treat it as a dropped detection and reset your UI. This can happen if the connection drops mid-speech.
Message History
Track conversation history:Real-time Transcription
Monitor speech in real-time:Director Notes Cues
Listen forDIRECTOR_NOTE_CUE_APPLIED when your application needs to react after Anam confirms that a Director Notes cue was applied to the avatar.
Director Notes cue events require
@anam-ai/js-sdk version 4.20.0 or later.correlationId to associate the cue with other events from the same user action.
Each MESSAGE_STREAM_EVENT_RECEIVED payload also includes an optional cueTag when a Director Notes cue takes effect from the start of that text chunk. If the field is absent, no new cue takes effect at that chunk boundary. Cue boundaries can split persona speech into multiple streamed text chunks, so append event.content in arrival order instead of assuming one event per response.
MESSAGE_HISTORY_UPDATED is unchanged. Its Message items do not include cue metadata.
Tool Call Handlers (Recommended)
UseregisterToolCallHandler to register handlers for specific tools before starting the session. This automatically emits completed or failed events when the handler completes.
Tool Call Events
Listen for tool call lifecycle events across all tool types — client, webhook, and knowledge tools all emit these events, so you can use them for logging, analytics, or monitoring:toolType and toolSubtype to distinguish between tool types in your listeners:
Server Warnings
Handle server-side warnings:Reasoning Events (Extended Thinking)
Track AI reasoning when using models with extended thinking:Removing Event Listeners
Remove listeners to prevent memory leaks, especially in single-page applications:React Example
Learn More
Client Tools Guide
Guide with examples for navigation, modals, UI updates, and more
Tools Overview
Learn about all tool types: client, webhook, and knowledge tools

