Overview
The application uses WebSocket to establish a persistent, bidirectional connection with AssemblyAI’s real-time transcription service. This enables low-latency audio streaming and immediate transcript delivery.Connection Setup
The WebSocket connection is established with query parameters specifying audio format and authentication:index.js
Connection Parameters
sample_rate=16000: Specifies the audio sample rate in Hzformatted_finals=true: Enables formatted final transcripts with punctuation and capitalizationtoken=${data.token}: Temporary authentication token for secure access
The sample rate must match the AudioContext configuration (16kHz). Mismatched sample rates will result in distorted audio and poor transcription accuracy.
Message Types
AssemblyAI’s real-time API uses typed messages to communicate transcription results:Turn Messages
The primary message type containing transcription results:index.js
Turn messages represent complete speech segments. The
turn_order field ensures turns can be displayed in the correct sequence even if messages arrive out of order.Turn Message Structure
Terminate Messages
Sent by the client to gracefully close the transcription session:index.js
Connection Lifecycle
1. Opening the Connection
index.js
- Logs the connection status
- Shows the message display element
- Starts audio recording and streaming
Always check
ws.readyState === WebSocket.OPEN before sending data to avoid errors from attempting to send on a closed or closing connection.2. Streaming Audio
Audio chunks are sent as binary data over the WebSocket:index.js
audioChunk is a Uint8Array containing 100ms of Int16 PCM audio data.
3. Receiving Transcripts
Transcripts arrive asynchronously as Turn messages:index.js
Turns are stored in an object keyed by
turn_order to handle out-of-order delivery. The display logic sorts turns before rendering.4. Error Handling
index.js
5. Closing the Connection
index.js
onclose handler fires when:
- The client sends a Terminate message and calls
ws.close() - The token expires
- Network connectivity is lost
- AssemblyAI terminates the connection
Connection State Management
The application uses a simple state management approach:index.js
Setting
ws = null after closing ensures the old WebSocket instance can be garbage collected and prevents accidental reuse of closed connections.Turn Ordering Algorithm
The application maintains correct turn order despite potential network delays:index.js
The
sort((a, b) => Number(a) - Number(b)) ensures numeric sorting. Without the Number() conversion, JavaScript would sort lexicographically (“10” before “2”).