4
votes

I would like to know how many people are currently connected to a room when using Twilio Video.

Twilio has a REST API to get a room resource, but it does not return current number of participants.

https://www.twilio.com/docs/api/video/rooms-resource#get-by-sid

Only way i see is to subscribe to status callback to "participant connected" and disconnected events and manually keep track of how many participants are connected or left the room.

Is there a better way to do this ?

2

2 Answers

3
votes

You can use twilio server side sdk, Let me share NodeJS example so you get better idea on implementation.

First lets define function that init twilio client and fetch connected participants of room.

async function getConnectedParticipants(roomName) {

  var Twilio = require('twilio');

  var apiKeySid = "YOUR_TWILIO_API_KEY_SID_HERE";
  var apiKeySecret = "YOUR_TWILIO_API_SECRET_HERE";
  var accountSid = "YOUR_TWILIO_ACCOUNT_SID_HERE";

  var client = new Twilio(apiKeySid, apiKeySecret, {accountSid: accountSid});

  var list = await client.video.rooms(roomName)
                      .participants
                      .list({status: 'connected'});

  return list;
}

Now let's use our function that return you connected participants.

var connectedParticipants = await getConnectedParticipants("YourRoomName");

// print all connected participants
console.log('connectedParticipants', connectedParticipants);

Note: I have used async and await in this example, please check more on that before implementation.

0
votes

Twilio developer evangelist here.

Keeping a server side list of the participants' identities based on the participant connected and disconnected events is probably the best way to work this out right now.

One alternative is to get this information from the front end. The JavaScript library allows you to query the participants in a room. You could periodically, or based on events, query that property and send it to your server via Ajax too.

Let me know if that helps.

Update

The Rooms API now allows you to retrieve information on participants that have connected to a room. To get the currently connected users in a room using Node.js, for example, the code would look like:

var client = new Twilio(apiKeySid, apiKeySecret, {accountSid: accountSid});

client.video.rooms(roomSid).participants
  .list({status: 'connected'}, (err, participants) => {
    if (err) { console.error(err); return; }
    console.log(participants.length);
  });