Skip to main content
WebSocket connections give you sub-100ms latency and bidirectional streaming, ideal for voice agents, live transcription, and any use case where audio flows continuously. If you don’t need real-time streaming, see HTTP vs. WebSocket instead. Prerequisites:
  • An SLNG key
  • Basic familiarity with WebSockets (open, send, receive, close)

Protocol overview

This page documents the WebSocket protocol used by SLNG-hosted models and bridges. Third-party providers (Deepgram, KugelAudio, Sarvam) may expose their own native WebSocket formats. Check the per-model API reference in the sidebar for provider-specific details.
Supported encodings, sample rates, and optional fields vary by model. For model-specific parameters, see the Text-to-Speech and Speech-to-Text tabs in the sidebar.

TTS WebSocket Protocol

For the full per-model parameter list and response schema, see the model’s page in the Text-to-Speech sidebar.

Connection

Use the direct provider path for proxied models and add the slng/ prefix for SLNG-hosted models:

Message Flow

Browse all available TTS models and endpoints on the Text-to-Speech models page.

Client → Server Messages

Initialize Session

Initialize a session with model and voice configuration before sending text.
Use config.pronunciation to set a default pronunciation dictionary for the session. Parameters:

Send Text for Synthesis

Send text to synthesize into audio. Set flush: true to finalize the current segment immediately instead of waiting for more text.
Use pronunciation on a text message to override the active dictionary for that turn. Later turns reuse the most recent active dictionary.

Flush Buffer

Force any buffered text/audio to be finalized and delivered.

Clear Buffer

Clear any queued text/audio from the current session.

Close Session

Close the current session and stop any further audio generation.

Server → Client Messages

Session Ready

Indicates the session is ready to receive messages.

Audio Chunk

Chunk of base64-encoded audio data.
Audio may also arrive as raw binary WebSocket frames instead of base64 JSON. Binary frames have lower overhead.

Segment Start

Signals the start of a synthesized segment.

Segment End

Signals the end of a synthesized segment.

Flushed

Acknowledges that buffered output was flushed.

Cleared

Acknowledges that queued output was cleared.

Audio End

Signals the end of audio generation.

Error

Indicates an error occurred during synthesis.
Common Error Codes:
  • auth_error: Invalid or missing SLNG key
  • config_error: Invalid configuration
  • rate_limit: Too many requests
  • provider_error: Upstream provider error

STT WebSocket Protocol

For the full per-model parameter list and response schema, see the model’s page in the Speech-to-Text sidebar.

Connection

Use the direct provider path for proxied models and add the slng/ prefix for SLNG-hosted models:

Message Flow

Browse all available STT models and endpoints on the Speech-to-Text models page.

Client → Server Messages

Initialize Session

Initialize a session with recognition configuration before streaming audio.
Parameters:

Send Audio Data

Stream an audio frame to be transcribed. After initialization, send audio in one of two formats:
Binary frames are recommended over base64-encoded JSON for lower overhead.
Binary frames: send raw PCM audio samples directly as binary WebSocket frames:
JSON messages with base64-encoded data:

Finalize Transcription

Force the server to finalize any buffered audio and return results. The connection stays open so you can continue streaming.

Close Stream

Signal that no more audio will be sent. The server processes any remaining audio, sends final results, then closes the connection.
Use finalize when you want to flush results mid-session (e.g., between utterances). Use close when you are done and want to end the session.

Keep-Alive

Send periodically during silence to prevent idle disconnection.

Server → Client Messages

Session Ready

Indicates the session is ready to receive audio.

Partial Transcript

Interim transcription result.

Final Transcript

Final transcription result with optional metadata.

Error

Indicates an error occurred during recognition.

Next Steps

Integration guide

Best practices and troubleshooting

TTS examples

JavaScript and Python code for real-time TTS

STT examples

JavaScript and Python code for real-time STT

Protocol comparison

HTTP vs. WebSocket: when to use each