Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 55 additions & 42 deletions examples/020-twilio-media-streams-node/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,49 +36,22 @@ function createApp() {
const protocol = req.headers['x-forwarded-proto'] === 'https' ? 'wss' : 'ws';
const streamUrl = `${protocol}://${host}/media`;

const twiml = `<?xml version="1.0" encoding="UTF-8"?>
<Response>
<Say>This call is being transcribed by Deepgram.</Say>
<Connect>
<Stream url="${streamUrl}" />
</Connect>
</Response>`;

res.type('text/xml').send(twiml);
const response = new twilio.twiml.VoiceResponse();
response.say('This call is being transcribed by Deepgram.');
response.connect().stream({ url: streamUrl });

res.type('text/xml').send(response.toString());
console.log(`[voice] New call → streaming to ${streamUrl}`);
});

app.ws('/media', async (twilioWs) => {
app.ws('/media', (twilioWs) => {
let dgConnection = null;
let dgReady = false;
let streamSid = null;
const mediaQueue = [];

console.log('[media] Twilio WebSocket connected');

dgConnection = await deepgram.listen.v1.createConnection(DEEPGRAM_LIVE_OPTIONS);

dgConnection.on('open', () => {
console.log('[deepgram] Connection opened');
});

dgConnection.on('error', (err) => {
console.error('[deepgram] Error:', err.message);
});

dgConnection.on('close', () => {
console.log('[deepgram] Connection closed');
});

dgConnection.on('message', (data) => {
const transcript = data?.channel?.alternatives?.[0]?.transcript;
if (transcript) {
const tag = data.is_final ? 'final' : 'interim';
console.log(`[${tag}] ${transcript}`);
}
});

dgConnection.connect();
await dgConnection.waitForOpen();

twilioWs.on('message', (raw) => {
try {
const message = JSON.parse(raw);
Expand All @@ -94,21 +67,23 @@ function createApp() {
break;

case 'media':
try {
if (dgConnection) {
if (dgReady && dgConnection) {
try {
dgConnection.sendMedia(Buffer.from(message.media.payload, 'base64'));
}
} catch {}

} catch {}
} else {
mediaQueue.push(message.media.payload);
}
break;

case 'stop':
console.log('[twilio] Stream stopped');
if (dgConnection) {
try { dgConnection.sendFinalize({ type: 'Finalize' }); } catch {}
try { dgConnection.sendCloseStream({ type: 'CloseStream' }); } catch {}
try { dgConnection.close(); } catch {}
dgConnection = null;
}
twilioWs.close();
break;

default:
Expand All @@ -122,7 +97,7 @@ function createApp() {
twilioWs.on('close', () => {
console.log('[media] Twilio WebSocket closed');
if (dgConnection) {
try { dgConnection.sendFinalize({ type: 'Finalize' }); } catch {}
try { dgConnection.sendCloseStream({ type: 'CloseStream' }); } catch {}
try { dgConnection.close(); } catch {}
dgConnection = null;
}
Expand All @@ -135,6 +110,44 @@ function createApp() {
dgConnection = null;
}
});

(async () => {
dgConnection = await deepgram.listen.v1.connect(DEEPGRAM_LIVE_OPTIONS);

dgConnection.on('open', () => {
console.log('[deepgram] Connection opened');
dgReady = true;
for (const payload of mediaQueue) {
try {
dgConnection.sendMedia(Buffer.from(payload, 'base64'));
} catch {}
}
mediaQueue.length = 0;
});

dgConnection.on('error', (err) => {
console.error('[deepgram] Error:', err.message);
dgReady = false;
});

dgConnection.on('close', () => {
console.log('[deepgram] Connection closed');
dgReady = false;
});

dgConnection.on('message', (data) => {
const transcript = data?.channel?.alternatives?.[0]?.transcript;
if (transcript) {
const tag = data.is_final ? 'final' : 'interim';
console.log(`[${tag}] ${transcript}`);
}
});

dgConnection.connect();
await dgConnection.waitForOpen();
})().catch((err) => {
console.error('[deepgram] Setup failed:', err.message);
});
});

app.get('/', (_req, res) => {
Expand Down
2 changes: 1 addition & 1 deletion examples/020-twilio-media-streams-node/tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,8 @@ function testMediaStreamFlow(port, audioData) {
if (ws.readyState !== WebSocket.OPEN) return;

if (offset >= audioData.length || offset >= MAX_BYTES) {
// 4. "stop" — call ended
ws.send(JSON.stringify({ event: 'stop', streamSid: 'MZ_ci_test' }));
setTimeout(() => ws.close(), 500);
return;
}

Expand Down