- Familiarity with the SLNG WebSocket protocol
- A working WebSocket connection (see the TTS or STT quick starts)
Best Practices
Items 1 and 2 are required for any production integration. Items 3-6 improve quality and resilience.1. Connection Management
Implement Reconnection Logic:2. Error Handling
Always handle errors gracefully:3. Buffer Management (TTS)
Use flush strategically to control latency vs. quality:4. Audio Format Consistency
Ensure your audio format matches configuration:5. Heartbeat/Keep-Alive
Send keep-alive messages to prevent idle disconnection. For STT sessions, use the built-inkeepalive message type. For TTS, use a WebSocket ping frame:
6. Interruption Handling (TTS)
SLNG Voice Agents handle interruptions automatically via
enable_interruptions. This only applies if you manage TTS WebSocket connections yourself.- Send
{ "type": "close" }to stop server-side generation and end the session - Send
{ "type": "clear" }to discard queued audio (keeps the session open) - Clear your local audio buffer and stop playback
Troubleshooting
For WebSocket error codes and fixes for connection drops, choppy audio, delayed transcriptions, and authentication failures, see Error Codes & Troubleshooting.Next Steps
WebSocket protocol
Message types, parameters, and connection URLs
Protocol comparison
HTTP vs. WebSocket: when to use each
TTS examples
JavaScript and Python code for real-time TTS
STT examples
JavaScript and Python code for real-time STT