WebSocket client for xAI realtime voice and streaming TTS.
xAI exposes realtime features over WebSocket (not gRPC):
wss://api.x.ai/v1/realtimefor full voice agents (bidirectional audio + text)wss://api.x.ai/v1/ttsfor bidirectional streaming TTS
This module uses Websockex. It is separate from the gRPC Xai.Client
because the transports are different.
Example: Streaming TTS
{:ok, pid} = Xai.Realtime.connect_tts(
api_key: System.get_env("XAI_API_KEY"),
voice: "eve",
codec: "mp3",
on_audio: fn audio_chunk -> play_audio(audio_chunk) end
)
Xai.Realtime.send_text(pid, "Hello from Elixir. ")
Xai.Realtime.send_text(pid, "This is streaming TTS.")
Xai.Realtime.send_text_done(pid)For full realtime voice agents, use connect_realtime/1 and handle events.
Summary
Functions
Close the connection gracefully.
Connect to the full realtime voice agent endpoint.
Connect for streaming TTS (text → audio chunks).
Send raw event (for advanced realtime use).
Send a text delta for TTS or conversation.
Signal end of text for current utterance (TTS).
Builds a text.clear event for barge-in.
Builds a text.delta event. Useful for testing and for users who want raw events.
Builds a text.done event.
Types
Functions
Close the connection gracefully.
Connect to the full realtime voice agent endpoint.
Similar options, plus session configuration sent after connect.
Connect for streaming TTS (text → audio chunks).
Options:
:api_key(required, or XAI_API_KEY env):voice- default "eve":language- default "en":codec- mp3, wav, pcm, etc.:sample_rate:on_audio- callback for base64-decoded audio chunks:on_event- callback for all JSON events (optional)
Send raw event (for advanced realtime use).
Send a text delta for TTS or conversation.
Signal end of text for current utterance (TTS).
Builds a text.clear event for barge-in.
Builds a text.delta event. Useful for testing and for users who want raw events.
Builds a text.done event.