Open a full-duplex voice session over WebSocket and receive subtitles and audio.
Prerequisites
Install Ruby SDK 1.2.0 and configure your API key. Replace WorkspaceId with your workspace ID in Singapore. Qwen-Audio-Live requires a workspace-specific domain.
Prepare input.pcm as 16 kHz, 16-bit little-endian, mono raw PCM. You can use FFmpeg to convert a recording.
Quick start
Save the code as example.rb and run ruby example.rb.
reply.pcm contains 24 kHz, 16-bit little-endian, mono audio. The example allows 30 seconds for the reply. In your app, wait for the full reply before closing the session.
Session options
| Option | Description |
|---|---|
model | Model ID for the connection. Use qwen-audio-3.1-live. |
front_model | Model that talks to the user. The example uses qwen-audio-4.0-realtime-preview. |
turn_detection_mode | The example uses duplex so the model can keep listening while it speaks. |
voice | Choose a voice supported by the frontend model. The example uses yunshu. |
delegation | The example uses Client mode. Set the backend model when you use Responses mode. |
Methods and events
| Method | Purpose |
|---|---|
connect | Open the connection, wait for session.created, and send session.update. |
append_audio | Send an audio frame. |
mute_input / unmute_input | Mute or unmute input. |
send_user_text / send_item | Add text or a custom item to the session. |
send_function_call_output | Submit the result of a frontend function call. |
send_thinking / send_instruction / send_commentary | Send task progress, instructions, or commentary to the frontend model. |
append_backend_item / create_backend_response | Append an item to the backend or trigger a backend response. |
send_raw_event | Send a Live client event. |
finish | Send session.close, wait for session.closed, and disconnect. |
abort | Disconnect immediately. Final session usage may still be pending. |
| Event or callback | Description |
|---|---|
on_session_ready | Fires when the session ID is available. Wait for session.updated before sending audio. |
on_user_transcript_delta / on_assistant_text_delta | User transcript or assistant subtitle deltas. |
on_assistant_audio_chunk | A chunk of reply audio. |
on_delegation_created | A delegation summary. |
on_backend_event | A backend event in Responses mode. |
on_function_call | A frontend function call. |
on_usage_updated / on_closed | A usage update or session closure. The close event includes final usage. |
on_raw_event / on_error | Other server events or errors. |
- The delegation summary contains
delegation_id,task_id,summary,offset_ms, andcontext. Use the delegation ID when sending task progress. usageexposes the latest usage, includingsecondsand the context window usage ratio when available. On normal closure,session.closedreports final usage.
session.input_audio.append. See WebSocket connection and Server events for the protocol and event fields.