Skip to main content

WebSocket connection guide

Use a WebSocket connection to access the Sambert real-time speech synthesis service. This topic covers the service endpoint, request headers, and interaction workflow.

The DashScope SDK currently supports only Java and Python. For other programming languages, use a WebSocket connection to communicate with the service. User guide: For model details and recommendations, see Speech synthesis.

Service endpoint

Sambert is available only in the Beijing region. The WebSocket service endpoint is: wss://{WorkspaceId}.cn-beijing.maas.aliyuncs.com/api-ws/v1/inference Replace {WorkspaceId} with your actual workspace ID.
The URL is fixed and must use the wss:// protocol. Set the Authorization header as described in Request headers.
Alibaba Cloud Model Studio has released a workspace-specific domain for the China (Beijing) region. The new dedicated domain delivers superior performance and higher stability for inference requests. We recommend migrating from dashscope.aliyuncs.com to {WorkspaceId}.cn-beijing.maas.aliyuncs.com.Replace {WorkspaceId} with your actual Workspace ID. The existing domain remains fully functional.

Request headers

Include the following headers in the request:

Parameter

Type

Required

Description

Authorization

string

Yes

Authorization token in the format Bearer <your_api_key>. Replace <your_api_key> with your actual API key.

user-agent

string

No

Client identifier for server-side request tracking.

X-DashScope-WorkSpace

string

No

The workspace ID of Alibaba Cloud Model Studio.

X-DashScope-DataInspection

string

No

Controls data inspection. Data inspection is enabled by default when this parameter is not set. Set it to enable to explicitly turn on data inspection. Don't set this parameter unless necessary.

Authorization is verified during the WebSocket handshake. If the API key is invalid or missing, the handshake fails with an HTTP 401/403 error.

Interaction workflow

image
For details about client and server events, see Client events and Server-sent events. The client and server interact in the following sequence:
  1. Establish a connection: The client establishes a WebSocket connection with the server.
  2. Start a task: The client sends a run-task event to start a task. Sambert sends all text to synthesize in a single run-task event.
  3. Wait for confirmation: The client receives a task-started event from the server, which indicates that the task has started.
  4. Receive audio: The client receives an audio stream from the server over the binary channel. The client also receives result-generated events that carry additional information such as timestamps.
  5. Complete the task: The client receives a task-finished event from the server, which indicates that the task is complete.
  6. Close the connection: The client closes the WebSocket connection.
To improve resource efficiency, reuse WebSocket connections for multiple tasks instead of creating a new connection for each task. For details, see Connection reuse (WebSocket).
Sambert supports output-only streaming (out), not bidirectional (duplex) streaming. All text to synthesize must be sent in a single run-task event. The continue-task and finish-task commands aren't supported.
Text Generation
Image Generation
Video Generation
World models
3D Model Generation
Audio
  • Audio generation
Realtime API
Text Embedding
Decision Model
TokenPlan
Model Production