Search code examples
node.jstwiliotwilio-api

How to get Conference Sid at the time of dialing twilio call


I've been working with twilio, using Node.js, and dialing call between two web end points. One is client and other is agent. I'm using following code to dial call.

    function dialCall(calledNumber, url) {

    client.calls.create({
        to: `client:${calledNumber}`,
        from: twilioNumber,
        url: url
      })
      .then(call => call.sid));
}

I'm using following twiml to establish a call.

 const generateTwiml = (conferenceName) => {
      let twimlResponse = new VoiceResponse();
  twimlResponse.say(`Welcome to unity dialer.`, {
    voice: 'alice',
  });
  const dial = twimlResponse.dial({
    timeLimit: '600',
  });
  dial.conference({
  startConferenceOnEnter: true,
  endConferenceOnExit: true
}, "Test Room");
  return twimlResponse.toString();
};

I've been successfully calling both agents and clients and getting callSid of both calls. However, my question is that at this point of time I also want to get conference Sid as well as I'm dialing the call as conference. What is the method to get that. As per documentation there is a method to fetch conference using conference name and status. However, if I use this some time the same is not returned due to race condition and I have to implement set time out function for same arbitrary delay. I've been getting the result but is there any other solution available for that.


Solution

  • Twilio developer evangelist here.

    At the time you return the TwiML to create the conference there is not yet a conference resource so there's no way to get the conference SID at that stage.

    As you describe, you can use the conference resource to list conferences and filter by the name you give it. However, you can't list the conferences at the time you return the TwiML because that conference hasn't been created by then.

    Rather than setting a timeout, which could be flaky, I recommend you use the statusCallback attribute of the <Conference> TwiML to set a URL to callback to when the conference starts. In the parameters to that callback you will get the ConferenceSid.