Skip to main content
This guide covers what you need to build a production-ready WebSocket integration. For message types and parameters, see the WebSocket protocol reference. Prerequisites:

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-in keepalive 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.
When building your own voice agent, stop TTS output as soon as the user starts speaking:
  • 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
For complete patterns (immediate interrupt, clear-and-restart, fade-out, voice agent loop), see TTS WebSocket examples.

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