node.jstwiliotwilio-twiml

How to end conference with node.js?


A conference is open with two participants. If someone hang up, the conference is still open, and I want that conference hang up if the amount of participants is lower then 2.

Any Ideas how to realize this in node.js?

This is the conference code:

resp.say({voice:'woman'}, 'You get to the conference.')
    .dial({},function(err){
        if(err){
            console.log(err);
        }
        this.conference('example');
    });

Solution

  • You can use the <Conference> attribute endConferenceOnExit="true" to stop the call if a participant leaves. In your code, that would look like:

    resp.say({voice:'woman'}, 'You get to the conference.')
        .dial({},function(err){
            if(err){
                console.log(err);
            }
            this.conference('example', { endConferenceOnExit: true });
        });
    

    In your case, with two people in the conference that will work as expected, however if more people join the conference with that attribute, then when one of them leaves the whole call will end. It's normal in this case to have a moderator (you, most likely, given the other questions I've seen you ask on SO recently :) have the attribute endConferenceOnExit="true" and the other participants have false (or not have the attribute, as it is false by default). That way, when you end your call, the whole conference will be finished, but if one of the participants ends the call it won't end for everyone.

    How does that sound?

    Edit

    OK, not the solution. In that case, you need to set up a callback for the <Dial> verb for each call to check, when someone hangs up, whether there is only one person left in the conference and hang up if it's less than two.

    You could do this like so:

    When you set up the initial conference TwiML using the <Dial> and then <Conference> you need to pass an action attribute to <Dial> with a URL as the argument, like so:

    resp.say({voice:'woman'}, 'You get to the conference.')
        .dial({ action: "/call_ended" },function(err){
            if(err){
                console.log(err);
            }
            this.conference('example');
        });
    

    Now, when a caller ends their call the action URL will receive a webhook from Twilio. We can then check the participants in the conference and end the conference, by setting the call status to complete, if there's only one person left.

    app.post("/call_ended", function(req, res, next) {
      // Find the conference by its name. You may already have the Conference SID stored
      client.conferences.list({ friendlyName: "example" }, function(err, data) {
        var conferenceSid = data.conferences[0].sid;
        // Find the participants left in the conference
        client.conferences(conferenceSid).participants.list(function(err, data) {
          if(data.participants.length < 2){
            // Loop over the remaining participants (should only be 1)
            data.participants.forEach(function(participant) {
              // Update the call and set the status to "completed"
              client.calls(participant.callSid).update({
                status: "completed"
              });
            });
          }
        });
      });
    });