Build a full-featured Anam application with advanced UI/UX and event handling
Create project directory
Initialize Node.js project
bash npm init -y
This creates a package.json
file for managing
dependencies.Create public directory
bash mkdir public
public
folder will contain your HTML and JavaScript files that are
served to the browser.Install dependencies
bash npm install express dotenv
Configure environment variables
.env
file in your project root to store your API key securely:your-api-key-here
with your actual Anam API key. Never commit this file to version control.Import the event types
Define your event listener function
Add your event listener to the client
script.js
file:
Event | Description |
---|---|
MESSAGE_HISTORY_UPDATED | Provides the complete conversation history each time someone finishes speaking |
MESSAGE_STREAM_EVENT_RECEIVED | Provides real-time transcription updates as speech occurs |
MESSAGE_HISTORY_UPDATED
event to build a basic chat history.
MESSAGE_HISTORY_UPDATED
event provides an array of message objects with
role
(“user” or “assistant”) and content
properties. This gives you the
complete conversation transcript each time someone finishes speaking.MESSAGE_STREAM_EVENT_RECEIVED
event:
MESSAGE_STREAM_EVENT_RECEIVED
event fires continuously as speech is
being processed. Use event.type
to distinguish between ‘persona’ (AI
speaking) and ‘user’ (user speaking) events.talk
method on the Anam client during a session.
script.js
file to send the command to the persona
streamToVideoElement
stream()
to capture and process persona outputstreamToVideoElement()
method accepts an optional audio input stream, allowing you to process or record user audio before sending it to the persona:
stream()
method which returns the raw output streams:
MESSAGE_STREAM_EVENT_RECEIVED
events that shows what the persona is saying as they speak, plus complete conversation history through MESSAGE_HISTORY_UPDATED
events.
Programmatic Control: Talk command functionality that allows you to send text messages directly to the persona, enabling you to react to user interactions and trigger automated responses.
Session Management: Secure server-side session token generation that properly handles API key protection and provides temporary access tokens for client-side connections.
Loading State Management: Responsive loading indicators that use the SESSION_READY
event to provide proper user feedback during connection establishment and session initialization.
Advanced Stream Recording: Manual stream management with separate user and persona audio recording capabilities, enabling conversation logging, quality assurance, and custom audio processing workflows.
getDefaultPersonaConfig()
method to customize:
app.css
to match your brand:
Connection Issues
Audio Problems
Performance Issues
UI Responsiveness