Skip to main content

ElevenLabs WebSocket TTS

ElevenLabsWebSocketTTS is a drop-in alternative to ElevenLabsTTS that streams audio over a WebSocket connection instead of the standard HTTP streaming endpoint. The public API is identical to the HTTP variant — the only difference is the transport.

Why use it

The WebSocket variant saves roughly 50 ms of HTTP setup + TLS cold-start per utterance compared to ElevenLabsTTS. On chatty conversational workloads this shaves real time off every agent turn. Use it when:
  • You are latency-bound on TTS time-to-first-byte.
  • Your model is one of the streaming-friendly eleven_flash_v2_5 / eleven_turbo_v2_5 families.
  • You do not need eleven_v3* (see Limitations).
If those conditions do not hold, stay on the HTTP variant — it is simpler and more forgiving on flaky networks.

Install

Usage

In an agent:

Constructor

Carrier auto-detect — setTelephonyCarrier

When you don’t know the carrier at construction time, StreamHandler calls setTelephonyCarrier(carrier) at call start to advise the provider of the wire format:
When outputFormat was passed explicitly to the constructor (or via forTwilio / forTelnyx), setTelephonyCarrier is a no-op — the user’s choice always wins. Calling with an unknown carrier ("" / "custom") is also a no-op.

Limitations

  • eleven_v3* is not supported. The v3 model family does not work over the WebSocket transport — fall back to the HTTP ElevenLabsTTS for those models.
  • Per-utterance lifecycle. A fresh WebSocket connection is opened and closed for each agent utterance. The latency win comes from a faster handshake, not from session reuse.
  • Network sensitivity. WebSocket connections are more sensitive to flaky links than short-lived HTTP requests. On unreliable networks the HTTP variant is safer.

Errors

ElevenLabsTTSError is thrown on:
  • Server-reported errors over the WebSocket frame.
  • Stalled connections (no audio frames received within the inactivity window).
  • Authentication failures (missing or invalid apiKey).