Best Practices
Session Management
Always Initialize Session First
Sendsession.init as the very first message after connecting to the WebSocket.
Secure Credential Storage
- Never hardcode credentials in client-side code
- Use environment variables for server-side applications
- Fetch credentials from your backend API for browser apps
- Rotate access tokens regularly
Session Lifecycle
Use the same session for multiple audio requests to improve performance:Proper Cleanup
There is no WebSocketsession.stop message. When you’re done:
- Close the WebSocket connection
- Leave the Daily.co room
- Call
POST /v1/sessions/{session_id}/endto release resources and mark the session ended
Audio Quality
Optimal Audio Format
Input audio should be:- Sample Rate: 16kHz by default, or send 48kHz and set
sample_rate: 48000inagent.speak - Channels: Mono (1 channel)
- Bit Depth: 16-bit signed integer
- Format: Raw PCM (no WAV headers)
- Encoding: Base64 string
Audio Normalization
Normalize audio levels to prevent clipping and ensure consistent volume:Noise Reduction
For best results, use clean audio without background noise:Chunk Size Recommendations
| Duration | Samples (48kHz) | Bytes (16-bit) | Base64 Size | Recommendation |
|---|---|---|---|---|
| 1 second | 48,000 | 96,000 | ~128 KB | Minimum |
| 5 seconds | 240,000 | 480,000 | ~640 KB | Optimal |
| 10 seconds | 480,000 | 960,000 | ~1.3 MB | Good |
| 30 seconds | 1,440,000 | 2,880,000 | ~3.8 MB | Maximum recommended |
Daily.co Connection
Wait for Session Confirmation
Wait for session to be established before sending audio:Monitor Avatar Participation
Track when the avatar joins the Daily.co room:Monitor Connection Health
Track Daily.co connection state and handle failures:Implement Reconnection Logic
Handle temporary network issues gracefully:Performance Optimization
Connection Pooling
Reuse WebSocket connections for multiple requests:Lazy Video Track Handling
Only process video frames when needed:Audio Buffering Strategy
For real-time applications, implement smart buffering:Error Handling
Comprehensive Error Handling
Handle all possible error scenarios:Validate Before Sending
Always validate data before sending:Troubleshooting
Daily.co Connection Issues
Problem: Cannot connect to Daily.co room or avatar video not appearing. Solutions:- Verify room credentials:
-
Check network connectivity:
- Verify internet connection is stable
- Daily.co handles firewall/NAT traversal automatically
- Monitor participant events:
- Add connection timeout:
Audio/Video Sync Issues
Problem: Audio and video are out of sync. Solutions:-
Check network latency and quality:
- Monitor Daily.co network events (connection quality, packet loss)
- Measure the time from sending
agent.speakto receivingagent.speak.confirmed(queueing latency)
-
Ensure consistent audio format:
- Always use 16-bit, mono PCM
- If you send 48kHz audio, include
sample_rate: 48000 - Verify mono channel
- Check 16-bit depth
- Monitor Daily.co video tracks:
Performance Issues
Problem: Slow video generation or high latency. Solutions:- Check audio format and size:
- Monitor network bandwidth:
- Optimize audio encoding:
Session Errors
Problem: “Session not found” or “Invalid session configuration” errors. Solutions:- Verify credentials are current:
- Check session hasn’t expired:
- Ensure avatar is configured:
WebSocket Disconnections
Problem: WebSocket connection drops unexpectedly. Solutions:- Handle reconnection:
- Check for network changes:
Error Reference
| Error Message | Meaning | Solution |
|---|---|---|
First message must be session.init | Session init was not sent first | Send session.init immediately after connecting |
Invalid room platform | The room config is missing/invalid | Set room.platform to daily |
Invalid video dimensions | Non-positive or non-numeric dimensions | Use positive integers for video_width and video_height |
Invalid aspect ratio | Unsupported width/height ratio | Use one of: 18:9, 16:9, 5:3, 16:10, 3:2, 4:3, 1:1 (or portrait equivalents) |
Invalid session id - server not assigned or session id mismatch | Session was routed to the wrong server | Re-run Start Session and use the returned ws_uri |
Failed to fetch video path | Session config could not be fetched | Ensure the session is started and the access_token is correct |
Session not found | Session ID not recognized | Re-establish session with session.init |
Invalid JSON format | Malformed JSON received | Validate JSON before sending |
No audio data provided | Empty audio field | Include base64-encoded audio |
Failed to process audio: ... | Invalid audio format | Verify 16-bit mono PCM and set the correct sample_rate |
Unknown message type: ... | Unsupported message | Check message type is supported |
Production Checklist
Before deploying to production:- Implement proper error handling for all message types
- Add reconnection logic with exponential backoff
- Validate audio format before sending (16-bit, mono PCM + correct
sample_rate, e.g. 48000) - Monitor Daily.co connection state and participant events
- Implement session lifecycle management
- Add logging and monitoring
- Test with various network conditions
- Handle WebSocket disconnections gracefully
- Secure credential storage (no hardcoding)
- Add timeout handling for all async operations
- Test audio/video synchronization in Daily.co room
- Optimize chunk sizes (5-10 seconds recommended)
- Don’t send application-level
pingmessages (unsupported); rely on reconnect logic - Add user-friendly error messages
- Test on target browsers/platforms
- Verify Daily.co room tokens are valid