goaudiotwiliotwilio-apitwilio-twiml

What is the real way to close a Twilio bidirectional audio stream?


I have a voice assistant that I want to use with the Twilio Voice API. I want to go back and forth, collecting user audio through a Gather and then using a bidirectional Stream to stream back the response. Something like this:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
   <Connect>
       <Stream url="wss://example.com/audiostream" />
   </Connect>
   <Gather></Gather>
</Response>

In the docs for the Twilio Voice API, it says Streams are blocking--specifically, "when <Connect><Stream> is used, Twilio does not executing the next set of TwiML instructions. The only way Twilio executes TwiML instructions after <Connect><Stream> is if your server closes the WebSocket connection with Twilio.", as we can see in the provided example:

<?xml version="1.0" encoding="UTF-8"?>
<Response>
   <Connect>
       <Stream url="wss://example.com/audiostream" />
   </Connect>
   <Say>This TwiML instruction is unreachable unless the Stream is ended by your WebSocket server.</Say>
</Response>

That's perfectly fine; in fact, it's desired behavior. However, elsewhere in the docs it says "For bidirectional Streams, the only way to stop a Stream is to end the call." That's very different, because it would prevent me from pursuing my plan of repeatedly spawning and killing websocket streams to facilitate my bot's speech.

Which is the truth?


Solution

  • I wrote a client to do this. Luckily the time wasn't wasted, as you can indeed perform the twiml sentence I envisioned by initiating the <Stream> and then closing it manually. That is, it is not the case that you can only close a bidirectional stream by ending the call altogether.