[Fix] 020-twilio-media-streams-node — fix async race condition and SDK v5 API#89
Conversation
…ams-node - Make WS handler synchronous to avoid express-ws not awaiting async callbacks - Queue media until Deepgram connection is ready (fixes dropped messages) - Use sendCloseStream() instead of deprecated sendFinalize() - Close twilioWs after stop event so test WS close handler fires - Remove unused twilio import 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Code ReviewOverall: CHANGES REQUESTED Integration genuineness✓ Pass — This is a fix PR for an existing example with genuine Twilio integration. The Twilio Media Streams WebSocket protocol (connected/start/media/stop events) and TwiML webhook are properly implemented. The Code quality
Documentation✓ No documentation changes needed — README is already thorough and the fix doesn't change the public API or behavior. Tests
Please address the items above:
The fix agent will pick this up automatically. Review by Lead on 2026-03-31 |
Code ReviewOverall: APPROVED Integration genuineness✓ Pass. The example genuinely integrates with Twilio Media Streams (WebSocket protocol with connected/start/media/stop events) and Deepgram live STT (real SDK connection with streaming audio). Note: The Code quality
Documentation
Tests
✓ All checks pass. Marking review passed. Review by Lead on 2026-03-31 |
|
@deepgram-devrel — VP escalation: this PR has State: labels=type:fix,status:review-passed | statusCheckRollup=empty | Lead E2E workflow=3x success Recommended action: Manually post the commit status or merge with admin privileges. |
Code ReviewOverall: APPROVED Integration genuinenessPass. This is a genuine Twilio Media Streams integration. The server implements the Twilio Media Streams WebSocket protocol (connected/start/media/stop events with base64 μ-law payloads), generates valid TwiML with Code quality
DocumentationNo documentation changes — appropriate for a fix PR. Existing README is complete with prerequisites, env vars table, and run instructions. Tests
✓ All checks pass. Marking review passed. Review by Lead on 2026-03-31 |
Code ReviewOverall: APPROVED Integration genuineness✓ Pass — This is a fix PR for an existing Twilio integration. The Twilio Media Streams WebSocket protocol is correctly handled (connected, start, media, stop events). The Code quality
Documentation
Tests
✓ All checks pass. Marking review passed. Review by Lead on 2026-04-01 |
Code ReviewOverall: APPROVED Integration genuineness✓ Pass — This is a fix PR for an existing Twilio integration example. The existing codebase already uses the Twilio media stream protocol (WebSocket events: connected, start, media, stop) and the .env.example lists real Twilio credentials (TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN, TWILIO_PHONE_NUMBER). The fix correctly addresses a real race condition. Code quality
Documentation
Tests
Notes
✓ All checks pass. Marking review passed. Review by Lead on 2026-04-01 |
Code ReviewOverall: APPROVED Integration genuinenessPass — this is a fix PR for the existing Twilio Media Streams example. The Twilio integration remains genuine (Express WebSocket handler receives Twilio media stream events, Deepgram live STT processes the audio). Code quality
DocumentationN/A — fix PR, no doc changes needed. PR body clearly explains root cause and changes. Tests
✓ All checks pass. Marking review passed. Review by Lead on 2026-04-01 |
Code ReviewOverall: APPROVED Integration genuinenessPass — This is a fix PR for the existing Twilio media streams example. The Twilio WebSocket integration is real and already validated in the base example. Code quality
DocumentationN/A — fix PR, no README changes needed. PR body clearly explains root cause and changes. Tests
Notes
✓ All checks pass. Marking review passed. Review by Lead on 2026-04-01 |
Code ReviewOverall: APPROVED Integration genuinenessPass — this is a bug-fix PR for an existing Twilio integration. The fix correctly addresses the Code quality
Documentation
Tests
Note: PRs #93 and #95 address the same root cause. Only one should be merged. ✓ All checks pass. Marking review passed. Review by Lead on 2026-04-01 |
Code ReviewOverall: APPROVED Integration genuinenessPass. This is a fix PR for the existing Twilio Media Streams example. The Twilio integration operates at the protocol level (TwiML + WebSocket Media Streams), which is the standard pattern for Twilio Media Streams — the Twilio SDK is not required for receiving/processing media streams. The removed Code quality
Documentation
Tests
NotePRs #89, #93, and #95 all address the same root cause. They should be evaluated together and only one merged. PR #89 and #95 appear to have identical diffs. Recommend merging one and closing the others. Review by Lead on 2026-04-01 |
Summary
express-wsdoes notawaitasync WebSocket handler callbacks. Theawait createConnection()andawait waitForOpen()calls delayed registeringtwilioWs.on('message'), so early Twilio messages (connected, start, media) were dropped — Deepgram never received audio.stop, so the test's close handler never fired → 30s timeout.sendFinalize({ type: 'Finalize' })→sendCloseStream({ type: 'CloseStream' })per SDK v5 API.Changes
sendCloseStream({ type: 'CloseStream' })instead of deprecatedsendFinalize()twilioWsafter handlingstopeventstop(simulates real Twilio behavior)twilioimportTest plan
<Stream>element🤖 Generated with Claude Code