Exchange SDP over a control WebSocket to send and receive real-time audio from Qwen-Audio-Live using WebRTC.
Send and receive audio via WebRTC media tracks. Session initialization and SDP negotiation are handled over the control WebSocket, which remains active throughout the session.
Select a region and URL as described in WebSocket access. The browser connects to your application server’s WebSocket proxy, which forwards JSON events between the browser and QwenCloud while carrying the API Key to connect to QwenCloud. Long-term API Keys are stored securely on your application server. The browser page is served over HTTPS or localhost
Even for voice-only use cases, the Offer SDP must include both
After the server returns session.updated over the control WebSocket, read the Answer SDP from
The example uses Responses mode. Prepare Node.js 20 or later, run
Save the code as
Save the following HTML as
The browser waits for
Run
For business integration, see Task Delegation and Tools. Request and response fields are documented in Client Events and Server Events.
Click End; the example invokes
Before you begin
Select a region and URL as described in WebSocket access. The browser connects to your application server’s WebSocket proxy, which forwards JSON events between the browser and QwenCloud while carrying the API Key to connect to QwenCloud. Long-term API Keys are stored securely on your application server. The browser page is served over HTTPS or localhost localhost and requests microphone permissions.
Connection flow
| Phase | Actions and Checkpoints |
|---|---|
| Open Control Connection | The browser connects to your application server’s proxy; the proxy uses the API Key to connect to QwenCloud. |
| Submit Offer SDP | The browser gathers ICE candidates and sends the Offer SDP to session.update over the control WebSocket. |
| Apply Answer SDP | Read the Answer SDP from session.updated’s session.transport.sdp and set it as the remote description. |
| Send and Receive Audio | After the media connection is ready, attach the microphone track. Upon receiving the remote audio track, verify that the player can render speech. |
Generate and send the Offer SDP
Even for voice-only use cases, the Offer SDP must include both m=video and m=application media sections. Adding a video transceiver and creating a DataChannel generate these two media sections respectively. Pure voice integration does not require camera capture or video transmission.
- Create
RTCPeerConnection, add ansendrecvaudio transceiver and a video transceiver, then create a DataChannel. In the example, the video transceiver is set torecvonly, and the DataChannel name isoai-events. - Call
createOffer()andsetLocalDescription(). Wait untiliceGatheringStatebecomescomplete, then read the complete Offer SDP—including ICE candidates—fromlocalDescription.sdp. - Send session.update over the control WebSocket. Set
session.transport.typetortcand write the Offer SDP intosession.transport.sdp.
iceServers: [] to gather local ICE candidates. If your deployment environment requires STUN or TURN, specify the corresponding server configuration in iceServers.
Apply the Answer SDP and start audio
After the server returns session.updated over the control WebSocket, read the Answer SDP from session.transport.sdp. Call setRemoteDescription({ type: "answer", sdp }) to apply the Answer. Once the media connection is established, attach the microphone track and pass the received remote audio track to the player.
Once the media connection is ready, receive server events over the control WebSocket or the WebRTC DataChannel. If both paths deliver an event with the same event_id, handle it once. For example, session.input_transcript.delta and session.output_transcript.delta can arrive on both paths.
Run the browser example
The example uses Responses mode. Prepare Node.js 20 or later, run npm install ws in an empty directory, and configure your QwenCloud API Key in DASHSCOPE_API_KEY. Copy the request URL from WebSocket access to UPSTREAM_URL below.
Create the application server
Save the code as server.mjs. The server hosts a local web page and establishes a QwenCloud control WebSocket for each browser connection. The example listens only on localhost.
The example proxy sends an additional X-Session-Id during the upstream WebSocket handshake, with its value prefixed by sess_ followed by 32 random hexadecimal characters.
Create the browser page
Save the following HTML as index.html. Then save the JavaScript below as live.js in the same directory.
proxy.ready from the local proxy before initiating SDP negotiation. The application receives subtitles via onTranscript and distinguishes between user and model output using type.
Verify the connection
Run node server.mjs, open http://localhost:8000, and click Start and allow microphone access. After the page displays "Media connected. You can speak now.", say a sentence and confirm that the player outputs the model’s voice reply. To start a new session, refresh the page. If subtitles appear but no audio plays, verify whether the track callback received the remote audio track, then check whether the browser blocked autoplay. If no Answer is received, first confirm that the Offer includes m=video and m=application, then inspect server-side errors. If an Answer is received but the media connection fails to establish, verify whether the Answer has been applied and whether the Offer contains ICE candidates usable on the current network.
Handle tasks and end the session
For business integration, see Task Delegation and Tools. Request and response fields are documented in Client Events and Server Events.
Click End; the example invokes closeSession() to send session.close. Upon receiving session.closed, stop audio capture and close the connection.