Turn Message Structure
AssemblyAI sends transcript messages with type"Turn" containing:
type: Message type identifier (“Turn” for transcripts)turn_order: Numeric identifier for ordering utterancestranscript: The transcribed text for this turn
Turns represent complete utterances. The
turn_order ensures you display them in the correct sequence, even if messages arrive out of order.Processing Turn Messages
Initialize turns storage
Create an object to store turns by their order:Using an object allows fast lookups and updates by turn order.
public/index.js
Set up message handler
Listen for WebSocket messages and parse the JSON:Message handling: Parse the JSON string from
public/index.js
event.data (line 2), check if it’s a Turn message (line 3), and filter out other message types AssemblyAI might send.Store turn by order
Extract and store the transcript:Destructuring: Extract
public/index.js
turn_order and transcript from the message (line 1), store the transcript using turn_order as the key (line 2), which overwrites if the same turn is updated.Sort and display turns
Sort turns numerically and join them for display:Processing pipeline:
public/index.js
- Get all turn_order keys from object (line 1)
- Sort keys numerically, not alphabetically (line 2)
- Map keys to their transcript values (line 3)
- Join all transcripts with spaces (line 4)
- Update the DOM element with complete text (line 6)
Why Sort Numerically?
JavaScript’s default sort is alphabetical, which causes issues:Number(a) - Number(b) for turn_order sorting to maintain correct sequence.
Complete Message Handler
Here’s the full implementation:public/index.js
Understanding Turn Order
Turns represent natural speech segments:- Created when speaker pauses or stops
- May arrive out of order due to processing time
- Can be updated if AssemblyAI refines the transcript
Handling Message Updates
Turns can be updated with refined transcripts:turn_order automatically handles updates by overwriting the previous value.
Display Best Practices
Joining Turns
Join with spaces for natural reading:Preserving Formatting
Withformatted_finals=true, transcripts include punctuation:
UI Updates
Update the display element with ordered transcripts:public/index.js
- Use
innerTextfor plain text (escapes HTML) - Updates happen on every Turn message
- Display reflects current state of all turns
Clearing Transcripts
When starting a new session:Complete Integration Example
Here’s how transcript handling fits into the full application:public/index.js
Message Type Reference
While this example focuses on Turn messages, AssemblyAI may send other types:"Turn": Complete transcript for an utterance"SessionBegins": Connection established"PartialTranscript": Intermediate results (if enabled)"FinalTranscript": Finalized results (if not using turns)
msg.type === "Turn" to handle only complete utterances.
Debugging Tips
Log incoming messages
Verify turn ordering
Check transcript updates
Summary
Key points for handling transcripts:- Store turns in an object keyed by
turn_order - Always sort numerically:
.sort((a, b) => Number(a) - Number(b)) - Join turns with spaces for natural reading
- Update UI on every Turn message
- Use
formatted_finals=truefor punctuated transcripts