4

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 ?

Koder
  • 1,794
  • 3
  • 22
  • 41

2 Answers2

3

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.

Rajesh Meniya
  • 753
  • 1
  • 7
  • 17
0

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);
  });
philnash
  • 70,667
  • 10
  • 60
  • 88
  • I want to restrict the number of people joining a room. I know twilio has option too as MaxCurrentParticipants, but i would like to do some custom checks on number of current participants, access level of those participants etc. It would be great if i can do a rest api call to /rooms/:roomId and get participants array so i can cross reference in my system and respond to request to join a room by creating/denying access token. Can't rely on client side ajax calls as they can be easily defeated. – Koder Sep 29 '17 at 12:31
  • I'm afraid that currently listening to the webhooks is the best way to tell this at the moment then. – philnash Oct 02 '17 at 10:11
  • I expect the list of participant to be returned in status callbacks and when retrieving rooms via the API. Keeping track of who connects/disconnects means more DB throughput and out of scope development time for us instead of just reading a property. This is an issue. – Quentin Hayot Feb 28 '18 at 14:52
  • Hey Quentin, good news, this is an old answer. You can now make REST API calls to a room's [participants resource](https://www.twilio.com/docs/api/video/participants). Check out the updated code in my answer above too. – philnash Mar 01 '18 at 02:25