Skip to main content

Adding Event Listeners

Register event listeners after creating the Anam client and before starting the session with stream() or streamToVideoElement().
Event handlers must be registered before the session starts. Startup events such as microphone permission, connection, stream, and SESSION_READY can fire while stream() or streamToVideoElement() is running, and late listeners will not receive events that already fired. This also applies to registerToolCallHandler.
Use the addListener method before the start call:

Available Events

Type Definitions

Message

MessageStreamEvent

DirectorNoteCueAppliedEvent

ToolCallStartedPayload

ToolCallCompletedPayload

ToolCallFailedPayload

ToolCallHandler

ClientToolEvent (Deprecated)

ClientToolEvent is deprecated as of SDK v4.9.0. Use ToolCallStartedPayload with registerToolCallHandler or TOOL_CALL_STARTED events instead.

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:
These fire as soon as the server-side VAD picks up voice activity, before the full transcript arrives via 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 for DIRECTOR_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.
Use 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. Use registerToolCallHandler to register handlers for specific tools before starting the session. This automatically emits completed or failed events when the handler completes.
registerToolCallHandler is the recommended approach for client tools. Register tool call handlers before stream() or streamToVideoElement() so the persona cannot invoke a tool before your handler is attached.

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:
Use toolType and toolSubtype to distinguish between tool types in your listeners:
These events fire for all tool types. For guides on configuring each type, see Client Tools, Webhook Tools, and Knowledge Tools.

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
Last modified on July 10, 2026