🎉 New Feature Available! Video interaction with streaming avatars is now live! You can now enable two-way video communication with your avatars, including camera switching capabilities and video quality controls. Check out the Video Interaction section for implementation details.
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 Agora SDK, LiveKit SDK, or TRTC SDK, including:- Setting up real-time communication channels
- Handling avatar interactions and responses
- Managing audio streams
- Implementing cleanup procedures
- Optional LLM service integration
Prerequisites
1. Install the SDK
2. Import the required dependencies
3. Understanding Data Channel Limitations
LiveKit SDK - Data Channel Limitations
LiveKit SDK - Data Channel Limitations
LiveKit uses WebRTC data channels for message communication with more generous limits:For more details, refer to LiveKit’s Data Channel Documentation.
Important: LiveKit data channel has the following limitations:
- Reliable mode: Maximum message size of 15 KiB (15,360 bytes) per message
- Lossy mode: Recommended to keep messages under 1,300 bytes
- For this integration, we use reliable mode which provides significantly larger message capacity than Agora
TRTC SDK - Data Channel Limitations
TRTC SDK - Data Channel Limitations
TRTC uses custom messages for communication with specific limitations:For more details, refer to TRTC’s Custom Message Documentation.
Important: TRTC data channel has the following limitations:
- Custom messages: Maximum message size of 1 KB (1,024 bytes) per message
- Rate limit: 30 calls per second, 8 KB/s for custom messages
- Messages are sent reliably and in order by default
Integration Flow
The integration follows the same pattern regardless of which SDK you choose (Agora, LiveKit, or TRTC):Key Implementation Steps
1. Create a Live Avatar Session
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 the Client/Room
Create and configure the client or room:3. Subscribe to 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:Agora: Handling Large Messages with Chunking
Agora: Handling Large Messages with Chunking
In real-world scenarios with Agora, the message size is limited to 1KB and the message frequency is limited to 6KB per second, so we need to split large messages into chunks and send them separately:
LiveKit Note: Unlike Agora’s 1KB limit, LiveKit supports messages up to 15 KiB in reliable mode, so chunking is generally not needed for typical conversational messages. For very large messages (over 15 KiB), you would need to implement similar chunking logic.TRTC Note: TRTC has the same 1KB message size limit as Agora, so you would need to implement similar chunking logic for large messages. The chunking approach shown above for Agora can be adapted for TRTC by replacing
client.sendStreamMessage() with client.sendCustomMessage().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:8. 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
9. 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