Anam personas communicate through a rich event system that lets you respond to connection changes, conversation updates, and user interactions. Understanding these events is key to building responsive, interactive applications.

How Events Work

The Anam SDK uses an event-driven architecture where your application can listen for specific events and react accordingly. This allows you to:

  • Update your UI based on connection status
  • Track conversation history in real-time
  • Handle user interruptions gracefully
  • Monitor stream quality and performance
1

Initialize Client

Create your Anam client with a session token

2

Add Event Listeners

Register listeners for the events you care about

3

React to Events

Update your UI and application state based on event data

4

Clean Up

Remove listeners when components unmount or sessions end

Available Events

Connection Events

These events track the connection lifecycle between your client and Anam’s streaming infrastructure:

CONNECTION_ESTABLISHED

Fired when the WebRTC connection is successfully established.

import { AnamEvent } from "@anam-ai/js-sdk/dist/module/types";

anamClient.addListener(AnamEvent.CONNECTION_ESTABLISHED, () => {
  console.log('Connected to Anam streaming service');
  updateConnectionStatus('connected');
  hideLoadingSpinner();
});

CONNECTION_CLOSED

Fired when the connection is terminated.

anamClient.addListener(AnamEvent.CONNECTION_CLOSED, () => {
  console.log('Connection closed');
  updateConnectionStatus('disconnected');
  showReconnectButton();
});

Video Events

VIDEO_PLAY_STARTED

Fired when the first video frames begin playing. Ideal for removing loading indicators.

anamClient.addListener(AnamEvent.VIDEO_PLAY_STARTED, () => {
  console.log('Video stream started');
  hideVideoLoadingState();
  showPersonaInterface();
});

Conversation Events

These events help you track and respond to conversation flow:

MESSAGE_HISTORY_UPDATED

Provides the complete conversation history each time a participant finishes speaking.

anamClient.addListener(AnamEvent.MESSAGE_HISTORY_UPDATED, (messages) => {
  console.log('Conversation updated:', messages);
  updateChatHistory(messages);
  
  // Example message structure:
  // [
  //   { role: "user", content: "Hello" },
  //   { role: "assistant", content: "Hi there! How can I help?" }
  // ]
});

MESSAGE_STREAM_EVENT_RECEIVED

Provides real-time transcription updates as speech occurs.

anamClient.addListener(AnamEvent.MESSAGE_STREAM_EVENT_RECEIVED, (event) => {
  if (event.type === 'persona') {
    // Persona is speaking - show real-time transcription
    updatePersonaTranscript(event.text);
  } else if (event.type === 'user') {
    // User finished speaking - complete transcription
    updateUserTranscript(event.text);
  }
});

Audio Events

INPUT_AUDIO_STREAM_STARTED

Fired when microphone input is successfully initialized.

anamClient.addListener(AnamEvent.INPUT_AUDIO_STREAM_STARTED, (stream) => {
  console.log('Microphone access granted');
  showMicrophoneIndicator();
  updateAudioInputStatus('active');
});

Talk Stream Events

TALK_STREAM_INTERRUPTED

Fired when a user interrupts a TalkMessageStream by speaking.

anamClient.addListener(AnamEvent.TALK_STREAM_INTERRUPTED, (event) => {
  console.log('Talk stream interrupted:', event.correlationId);
  handleStreamInterruption(event.correlationId);
  stopCurrentGeneration();
});