跪拜 Guibai
← All articles
WebSocket · Node.js · Frontend

Streaming TTS vs. Regular TTS: WebSocket Frames, Signed URLs, and Write Streams, End to End

By YIAN ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

Voice assistants and LLM speech output depend on streaming TTS because the first audio arrives while text is still being generated; regular TTS forces the user to wait for the whole utterance. The practical stakes for Node.js developers are a WebSocket protocol with signed URLs, binary frames, and a race condition on duplicate ready events that must be handled correctly.

Summary

Tencent Cloud's WebSocket streaming TTS works differently from ordinary text-to-speech: instead of one HTTP request returning a Base64 audio string, the client keeps a long-lived connection open, sends text in segments, and receives MP3 binary frames as soon as synthesis produces them. The signed WSS URL is built client-side by sorting query parameters alphabetically and computing an HMAC-SHA1 signature with SecretKey. The session then follows a fixed event order: server sends ready=1, client sends ACTION_SYNTHESIS frames, server streams binary audio chunks, client sends ACTION_COMPLETE, and server closes with final=1.

Handling those frames requires branching on WebSocket message type. Text frames carry JSON control messages and need Buffer.toString() plus JSON.parse(); binary frames are raw audio and must go straight into a file stream, not through Base64 decoding. A sent guard must be set before an awaited sendTexts(), because a duplicate ready=1 during the wait would otherwise start a second send. Seven common misconceptions are called out, including treating each audio chunk as its own file, thinking return closes the socket, and assuming node:crypto needs installation.

Takeaways
— Regular TTS uses one HTTP request, sends full text, and returns a Base64-encoded audio string inside JSON.
— Streaming TTS uses a WebSocket long connection, sends text in segments, and receives audio as binary frames while synthesis is in progress.
— Binary frames from streaming TTS are raw audio bytes and must be written directly to a file stream; Base64 decoding is only for regular TTS JSON responses.
— Control messages such as ready=1 and final=1 arrive as text frames; in Node.js the message data is a Buffer and must be converted to a string before JSON.parse().
— The WSS URL is generated client-side: parameters are sorted alphabetically, signed with HMAC-SHA1 using SecretKey, and appended to wss://tts.cloud.tencent.com/stream_wsv2.
— The value of session_id in WebSocket messages must match the SessionId parameter in the signed URL, even though the field names differ in case.
— Set the sent/textStreamStarted flag before awaiting sendTexts() so a duplicate ready=1 cannot launch a second send loop.
— All audio chunks are appended to one file through fs.createWriteStream with flags 'w', which overwrites the old file on each run.
Conclusions

The seven-misconception list groups the failure modes most likely to trip up developers new to streaming TTS, and most of them come from carrying over HTTP/JSON assumptions into a WebSocket protocol.

The sent flag is a small race-condition fix with a big consequence: an await inside sendTexts() means a delayed ready frame can otherwise duplicate the entire synthesis request. The tutorial's insistence on setting the flag before the await is the whole point.

The comparison between Base64 strings and binary frames explains why streaming TTS feels faster: audio never gets re-encoded as text, so the client can write chunks as they arrive.

Concepts & terms
Streaming TTS
A text-to-speech mode that maintains a WebSocket connection, sends text in pieces, and receives synthesized audio chunks as they are generated, reducing first-audio latency.
Binary frame
A WebSocket message type that carries raw bytes, used here for MP3 audio chunks, requiring no Base64 decoding.
Text frame
A WebSocket message type carrying UTF-8 JSON control messages such as ready, final, and error codes.
HMAC-SHA1 signature
An authentication hash computed over sorted request parameters plus request method, host, and path using a SecretKey, used to build the WSS URL.
Write stream
Node.js fs.createWriteStream used to append incoming audio chunks to a single output file.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗