Twilio websocket stream endpoint

637 Views Asked by At

I'm trying to create a websocket connection during the call. I have a flex function:

const VoiceResponse = require('twilio').twiml.VoiceResponse;

exports.handler = function(context, event, callback) {
    let voiceResponse = new VoiceResponse();
    const response = new Twilio.Response();
    response.appendHeader('Access-Control-Allow-Origin', '*');
    response.appendHeader('Access-Control-Allow-Methods', 'OPTIONS POST GET');
    response.appendHeader('Content-Type', 'application/json');
    response.appendHeader('Access-Control-Allow-Headers', 'Content-Type');
    
    const start = voiceResponse.start();
    const stream = start.stream({
        name: event.taskSid, 
        url: 'wss://....ngrok.io',
        
    });
    stream.parameter({
       name:'track',
       value: 'both_tracks' 
    });

    console.log(voiceResponse.toString());
    callback(null, response);
};

And in my flex ui project I call this function on the event:

  Actions.addListener('afterAcceptTask', (payload) => {
      if (isInboundCall(payload)) inboundCall(payload);          
  });

I've got a xml response in function. But I can't get any response at my websocket server. May be I'm calling my function from the wrong event?

1

There are 1 best solutions below

0
On

Have you tried to take a look at the REST API. Returning TwiML only works if Twilio is requesting TwiML.

Start/Stop Media Streams API https://www.twilio.com/changelog/startstop-media-streams-api