Why WebSocket for Conversational AI?
Traditional REST APIs require a new HTTP request for each TTS call. WebSocket maintains a persistent connection, enabling:- ~300ms first chunk latency - Users hear responses almost immediately
- Continuous conversation - No connection overhead between turns
- Multiple concurrent requests - Handle overlapping synthesis smoothly
- Real-time streaming - Audio plays as it’s generated
Building a Voice Agent
Here’s a complete example of a conversational AI agent using WebSocket TTS:Handling Multiple Speakers
For conversations with multiple voices:Integration with LiveKit
The WebSocket API works perfectly with LiveKit agents. Here’s how it’s implemented in the Uplift TTS plugin:Best Practices
Important: Unlike ElevenLabs, you cannot append text to the same request ID. Each synthesis requires a unique request ID.
Do’s
- ✅ Generate unique request IDs for each synthesis
- ✅ Cancel ongoing synthesis before starting new one
- ✅ Buffer 2-3 chunks before starting playback
- ✅ Implement reconnection logic with exponential backoff
Don’ts
- ❌ Don’t reuse request IDs
- ❌ Don’t create multiple connections for single conversation
- ❌ Don’t wait for all chunks before playing (increases latency)
Performance Tips
- Start playback early: Begin audio playback after receiving 2-3 chunks for lowest perceived latency
- Preload voices: The first request with a new voice may take slightly longer
- Use MP3_22050_32: Best balance of quality and bandwidth for streaming
- Single connection: Maintain one WebSocket for entire conversation session
Next Steps
WebSocket API Reference
Complete API documentation
LiveKit Voice Agent
Build a complete voice agent
