Streaming Avatar Integration: using Agora SDK
Learn how to integrate streaming avatars using the Agora SDK
Overview
The Streaming Avatar feature allows you to create interactive, real-time avatar experiences in your application. This guide provides a comprehensive walkthrough of integrating streaming avatars using the Agora SDK, including:
- Setting up real-time communication channels
- Handling avatar interactions and responses
- Managing audio streams
- Implementing cleanup procedures
- Optional LLM service integration
The integration uses Agora’s Real-Time Communication (RTC) SDK for reliable, low-latency streaming and our avatar service for generating responsive avatar behaviors.
Prerequisites
- Install the Agora SDK in your project:
- Import the required dependencies:
- Add the hidden API of Agora SDK
Agora SDK’s sendStreamMessage is not exposed, so we need to add it manually. And it has some limitations, so we need to handle it carefully.
We can infer from the doc that the message size is limited to 1KB and the message frequency is limited to 6KB per second.
The Agora SDK’s sendStreamMessage
method needs to be manually added to the type definitions:
Important: The Agora SDK has the following limitations:
- Maximum message size: 1KB
- Maximum message frequency: 6KB per second
Integration Flow
Key Implementation Steps
1. Create a Live Avatar Session
Security Recommendation: We strongly recommend implementing session management through your backend server rather than directly in the browser. This approach:
- Protects your AKool API token from exposure
- Allows for proper request validation and rate limiting
- Enables usage tracking and monitoring
- Provides better control over session lifecycle
- Prevents unauthorized access to the API
First, create a session to obtain Agora credentials. While both browser and backend implementations are possible, the backend approach is recommended for security:
2. Initialize Agora Client
Create and configure the Agora client:
3. Subscribe Audio and Video Stream
Subscribe to the audio and video stream of the avatar:
4. Set Up Message Handling
Configure message listeners to handle avatar responses:
5. Send Messages to Avatar
Implement functions to interact with the avatar:
In real-world scenarios, the message size is limited to 1KB and the message frequency is limited to 6KB per second, so we need to split the message into chunks and send them separately.
6. Control Avatar Parameters
Implement functions to control avatar settings:
7. Audio Interaction With The Avatar
To enable audio interaction with the avatar, you’ll need to publish your local audio stream:
Now you can integrate audio controls into your application:
For more details about Agora’s audio functionality, refer to the Agora Web SDK Documentation.
8. Video Interaction With The Avatar (coming soon)
Video interaction is currently under development and will be available in a future release. The following implementation details are provided as a reference for upcoming features.
To enable video interaction with the avatar, you’ll need to publish your local video stream:
The upcoming video features will include:
- Two-way video communication
- Camera switching capabilities
- Video quality controls
- Integration with existing audio features
Stay tuned for updates on when video interaction becomes available.
9. Integrating your own LLM service (optional)
You can integrate your own LLM service to process messages before sending them to the avatar. Here’s how to do it:
Remember to:
- Implement proper rate limiting for your LLM service
- Handle token limits appropriately
- Implement retry logic for failed LLM requests
- Consider implementing streaming responses if your LLM service supports it
- Cache common responses when appropriate
10. Cleanup
Cleanup can also be performed either directly or through your backend:
When implementing through your backend, make sure to:
- Securely store your AKool API token
- Implement proper authentication and rate limiting
- Handle errors appropriately
- Consider implementing session management and monitoring
11. Putting It All Together
Here’s how to use all the components together: