Describes how to connect to the Multimodal Development Kit (multimodal-dialog) in a browser using WebRTC and JavaScript to enable real-time audio/video interaction with a multimodal AI application.
Prerequisites and notes
-
Complete the following in the Alibaba Cloud Model Studio console:
- Configure an API key and set it as an environment variable.
- Create a Multimodal Development Kit application and obtain the Workspace ID and App ID (for details, see Create an application).
- In the application, configure the model, voice, prompt, agents and plug-ins, and other settings (for details, see Application configuration).
- Use a modern browser that supports WebRTC (Chrome, Edge, Firefox, Safari, and so on).
- The browser must have microphone permission. For video interaction, camera permission is also required.
- Browsers cannot directly send SDP exchange requests to the server due to CORS restrictions. In the demo, run the curl command in a terminal to complete the connection. In production, this restriction does not apply when the request is proxied through a backend service.
Implement real-time calls
The following sequence diagram shows the complete WebRTC real-time call flow:
WebRTC Multimodal Development Kit real-time call sequence diagram
Create RTCPeerConnection
Call the browser's native RTCPeerConnection constructor to create a connection instance. The server uses ICE-lite direct mode, so no ICE servers need to be configured.
Get local media streams
Use navigator.mediaDevices.getUserMedia to request microphone access (required) and camera access (optional).
Audio-only mode:
Add media tracks to PeerConnection
Add the local audio track to the PeerConnection. If video is enabled, add the video track as well.
Create DataChannel
Create a DataChannel named oai-events to exchange control messages with the server (run-task, event notifications, and so on).
pc.ondatachannel to handle DataChannels created by the server:
Generate Offer SDP
Call createOffer and setLocalDescription. The browser generates an Offer SDP that describes the local media capabilities. Since the server uses ICE-lite mode, you do not need to wait for ICE candidate collection — you can send the Offer SDP immediately after setLocalDescription.
Exchange SDP (HTTP POST)
Send the Offer SDP to the server WebRTC endpoint via HTTP POST. The server returns an Answer SDP.
Endpoint format: {workspace_id}.{region}.maas.aliyuncs.com, where workspace_id is the Alibaba Cloud Model Studio workspace ID (for example, llm-xxxxxxxxxx) and region is the deployment region (for example, cn-beijing). You can find these values in the Alibaba Cloud Model Studio console after creating a workspace.
Request configuration:
Field | Description |
|---|---|
URL |
|
Content-Type |
|
Header |
|
Body | Client-generated Offer SDP string |
Response | Success: HTTP 200 with the server's Answer SDP string |
Set Answer SDP to establish connection
Set the Answer SDP received from the server as the remote description. The WebRTC connection is then established.
Send run-task to start session
After the WebRTC connection is established and the DataChannel is ready, the client sends a run-task message to start the multimodal session.
Parameter path | Type | Description |
|---|---|---|
| string | Alibaba Cloud Model Studio workspace ID, obtained from the console under Application Management |
| string | Multimodal Development Kit application ID, obtained from the console under Application Management |
| string | Fixed value: |
| string | User identifier in your business system, used for log tracing |
| string | Device unique identifier, used for device-level analytics |
| string | Interaction mode: |
| string | Upstream media type: |
| string | Upstream audio sample rate, typically |
| string | Downstream voice; select from the voice list in the Alibaba Cloud Model Studio console |
| number | Downstream audio sample rate, typically |
| string | Optional. Vocabulary ID to improve recognition accuracy for domain-specific terms |
workspace_id and app_id; the server automatically loads the corresponding configuration.Real-time conversation
After the run-task is sent, the session enters real-time conversation mode:
- Upstream: Audio/video captured by the browser is automatically sent to the server via RTP
- Downstream audio: AI voice responses are received through the
ontrackcallback and played back - Downstream events: Business events are received through the DataChannel
upstream.type is set to AudioAndVideo, the client is already sending a video track over WebRTC. When the server needs it (for example, when the AI agent determines it needs to "see" the scene), it sends an open_videochat event. The client must reply with continue-task to confirm that the video channel is open. This mechanism enables on-demand video processing, which conserves server resources.End session and release resources
After the call ends, release all resources to avoid memory leaks and device occupation.
Important notes
- API key security: Never hard-code your API key in front-end code. In production, proxy the SDP exchange request through a backend service and keep the API key on the server side only.
- CORS restrictions: Browsers cannot directly call the Alibaba Cloud Model Studio API for SDP exchange. In production, forward requests through a backend proxy.
- HTTPS requirement:
getUserMediarequires the page to be served over HTTPS in non-localhost environments. - Interaction mode: The Multimodal Development Kit supports three interaction modes —
duplex(full-duplex, user can interrupt at any time),push2talk(push-to-talk), andtap2talk(tap-to-talk). Choose the appropriate mode for your hardware. - Video frame rate: For video understanding scenarios, 15–30 fps is recommended. If only occasional image recognition is needed, reduce the frame rate to save bandwidth.
- Browser compatibility: Chrome 90+, Edge 90+, Firefox 85+, and Safari 15+ are recommended.
- Single-instance restriction: Maintain only one
RTCPeerConnectioninstance per page at a time. Close the existing connection before creating a new session.
Download the complete demo
The following is a complete HTML demo that you can open directly in a browser to experience multimodal interaction. Because of browser CORS restrictions, the SDP exchange is completed manually via a curl command.
webrtc_multimodel_demo.html
Steps:
- Open the file in a browser.
- Fill in the connection configuration: Endpoint (format:
{workspace_id}.{region}.maas.aliyuncs.com), API key, Workspace ID, and App ID. - To enable video interaction, select Enable video.
- Click Start session and grant the browser access to the microphone (and camera).
- If the browser can send the request directly (no CORS restrictions), the connection is established automatically. Otherwise, the page displays a curl command — copy it to a terminal, run it, and paste the returned Answer SDP back into the page.
- Once connected, speak into the microphone to start a real-time conversation with the multimodal AI.
- After the call, click Download remote audio to save a recording of the AI responses.