- 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
Connection Drops
Connection Drops
Problem: WebSocket disconnects unexpectedly
- Implement reconnection logic with exponential backoff (see Connection Management above)
- Send periodic keep-alive messages to prevent idle timeouts (see Keep-Alive)
- If behind a corporate proxy, confirm it supports WebSocket upgrades (
Connection: Upgradeheader) - Run a WebSocket echo test (
wscat -c wss://echo.websocket.org) to rule out local network issues
Audio Glitches (TTS)
Audio Glitches (TTS)
Problem: Choppy or distorted audio playback
- Buffer at least 200ms of audio before starting playback to absorb network jitter
- Use the WebAudio API (
AudioContext) instead of<audio>elements for gapless chunk playback - Confirm the sample rate in your audio player matches the
sample_ratefrom yourinitconfig - 24kHz linear16 audio requires ~384 kbps; verify your connection can sustain this
Delayed Transcriptions (STT)
Delayed Transcriptions (STT)
Problem: Transcription results lag behind audio
- Send audio in 20-100ms chunks (320-1600 bytes at 16kHz linear16) rather than large buffers
- Measure round-trip time with
Date.now()around send/receive to isolate network vs. server latency - Confirm
encodingandsample_ratein yourinitconfig match your actual audio format - For real-time use, prefer Deepgram Nova which is optimized for streaming latency
Authentication Failures
Authentication Failures
Problem: Connection rejected with 401
- Pass the API key as a header during the WebSocket handshake:
Authorization: Bearer YOUR_KEY - Verify your key is active in the SLNG dashboard
- Check for trailing whitespace or newlines in the key string
- Some WebSocket libraries don’t support custom headers; pass the key as a query parameter (
?token=YOUR_KEY) if needed
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