[Fix] 020-twilio-media-streams-node — fix async race dropping Twilio audio#95
Conversation
The Twilio WebSocket message handler was registered after `await dgConnection.waitForOpen()`, causing early media events to be dropped when the Deepgram connection took time to establish. This meant no audio reached Deepgram, resulting in zero transcripts and a 30s timeout. Changes: - Make WS handler synchronous; move Deepgram setup into async IIFE - Register Twilio message handler immediately, buffer audio in mediaQueue - Flush buffered audio once Deepgram connection opens - Use sendCloseStream (SDK v5 convention) instead of sendFinalize - Remove unused twilio dependency - Close Twilio WS after stop event (matches real Twilio behavior) - Fix test to close WS after sending stop 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Code ReviewOverall: APPROVED Integration genuinenessPass — This is a fix PR for the existing 020-twilio-media-streams-node example. The Twilio integration is genuine: the server receives real Twilio Media Stream WebSocket events and forwards audio to Deepgram. No integration changes — this fixes a race condition. Code quality
DocumentationN/A — fix PR, no documentation changes needed. Tests
Note on duplicate PRsPRs #89, #93, and #95 all fix the same root cause in the same file. #89 and #95 are nearly identical (both APPROVED). Only one should be merged — the others should be closed. Recommend merging #95 as the latest and closing #89 and #93. ✓ All checks pass. Marking review passed. Review by Lead on 2026-04-01 |
Code ReviewOverall: APPROVED Integration genuinenessPass — bug-fix PR for existing Twilio integration. Same fix as PR #89 (identical diff). Correctly addresses the async race condition in the WebSocket handler. Code quality
NoteThis PR is a duplicate of #89 — both have identical diffs. Only one should be merged. ✓ All checks pass. Marking review passed. Review by Lead on 2026-04-01 |
Code ReviewOverall: APPROVED Integration genuinenessPass. Same analysis as PR #89 — this is a fix PR for the Twilio Media Streams example. The Twilio integration is at the protocol level (TwiML + WebSocket), which is the standard pattern. The removed Code quality
Documentation
Tests
NoteThis PR has an identical diff to PR #89. Recommend merging only one of #89, #93, or #95 and closing the other two. Review by Lead on 2026-04-01 |
Code ReviewOverall: CHANGES REQUESTED Integration genuineness
Code quality
Documentation
Tests
NoteThis PR is nearly identical to #89. PRs #89, #93, and #95 all fix the same root cause. Recommend closing duplicates and consolidating into one PR. PR #93 has the most robust Please address the items above. The fix agent will pick this up automatically. Review by Lead on 2026-04-01 |
…-twilio-media-streams-node - Use twilio.twiml.VoiceResponse instead of hand-built XML string - Restore twilio dependency in package.json - Set dgReady=true inside the Deepgram 'open' callback (not after waitForOpen) - Reset dgReady=false on Deepgram close/error to prevent infinite queuing 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
Fix appliedRoot cause: Review requested three changes: use Twilio TwiML builder instead of hand-built XML, set dgReady in the open callback instead of after waitForOpen, and reset dgReady on close/error. Change: Added Tests will re-run automatically. Fix by Lead on 2026-04-01 |
Code ReviewOverall: APPROVED Integration genuinenessN/A — this is a bug fix PR, not a new example. Code quality
Tests
NotePRs #89, #93, and #97 address the same root cause. This PR (#95) is the most complete fix — recommend closing the others as duplicates. ✓ All checks pass. Marking review passed. Review by Lead on 2026-04-01 |
Fix: 020-twilio-media-streams-node regression
Root cause: The
app.ws('/media', async (twilioWs) => {...})handler awaiteddgConnection.waitForOpen()before registeringtwilioWs.on('message', ...). Sinceexpress-wsdoes not await async handlers, early Twilio messages (connected,start, andmediaaudio frames) arrived before the handler was registered and were silently dropped. No audio reached Deepgram, so no transcripts were returned, causing a 30-second timeout.Changes
src/index.js:express-wsdoes not await async callbacksmessage,close,errorhandlers immediatelymediaQueueuntil Deepgram connection is ready.catch()for error handlingwaitForOpen()resolves anddgReadyis setsendCloseStream({ type: 'CloseStream' })per SDK v5 convention (replaces deprecatedsendFinalize)twilioimport and dependencystopevent (matches real Twilio behavior)tests/test.js:stopevent (matches real Twilio client behavior)package.json:twiliodependencyTest plan
Fixed on 2026-04-01