Twilio-video.js: room.disconnect() for all participants

Created on 19 Dec 2017  路  8Comments  路  Source: twilio/twilio-video.js

Hi There,
I have a quick question that how can I close/disconnect all participants if the host leave the conversation.

I took an example from quick start example on disconnected function I saw room.disconnect();
but its disconnected from LocalParticipant. So is there any possibility to close all?

question

Most helpful comment

@ihatecodinglikeking alternatively, I wonder, would you like an API like

remoteParticipant.disconnect()

Hi @markandrus I'd love this API. I wonder if it's implemented somewhere or is it just hypothetical.
Thanks.

All 8 comments

You can use the status callback to determine when the host(participant) leaves https://www.twilio.com/docs/api/video/status-callbacks and then end the room via the REST API https://www.twilio.com/docs/api/video/rooms-resource#complete-room.

@idelgado Thanks for your help
I am using Javascript 1.x
but i am not sure how can i implement rest API, do you have any example with javascript ?

Hi @ihatecodinglikeking,

The code sample @idelgado shared is one way. Assuming you already have an app server serving as your "Access Token server" (like the one in the twilio/video-quickstart-js), you could add an HTTP endpoint to that server, and then let your client app POST to it. For example鈥攁nd I haven't tested this at all鈥攕omething like this (using express):

// server.js
const apiKeySid = 'SKXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX';
const apiKeySecret = 'your_api_key_secret';
const accountSid = 'AC1314d19f60762b63d29a8e2894e0f97a';

const express = require('express')
const Twilio = require('twilio')

const app = express()
const client = new Twilio(apiKeySid, apiKeySecret, { accountSid })

// ...

app.post('/rooms/:roomSid', async (req, res) => {
  const { roomSid } = req.params
  const { status } = req.query
  if (status !== 'completed') {
    res.status(400).end()
  }
  try {
    await client.video.rooms(roomSid).update({ status })
    res.status(200).end()
  } catch (error) {
    console.error(error.stack)
    res.status(500).send(error)
  }
})

// ...

Then, in the client app:

// client.js

// ...

async function doDisconnect(room) {
  // Disconnect the LocalParticipant.
  room.disconnect()

  // Complete the Room, disconnecting all RemoteParticipants.
  try {
    await fetch(`/rooms/${room.sid}?status=completed`, { method: 'POST' })
  } catch (error) {
    console.error(error)
  }
}

// ...

If you go this route, keep in mind:

  1. You should not check your credentials into your repo and you should not include them in the client application鈥攖hey should be kept at the app server.
  2. You should protect your app server's URLs for vending Access Tokens and this new URL, should you implement it, from unauthorized access.

@ihatecodinglikeking alternatively, I wonder, would you like an API like

remoteParticipant.disconnect()

that allowed you to disconnect a RemoteParticipant? (Assuming we had a permissions model for restricting this only to "moderators" or something like that?) It would let you implement your use case like

function doDisconnect(room) {
  room.participants.forEach(remoteParticipant => {
    remoteParticipant.disconnect()
  })
  room.disconnect()
}

Or would you even prefer to set the Room to "completed" directly? e.g.,

room.setCompleted()

Is there a particular API you'd prefer here?

@markandrus Thanks for your reply.
In my case, the initiator or creator should have a special feature like complete meeting/room
so once creator close the meeting everyone in that room should disconnected from the room

I think for now i am using group but with 1:1 only so i use doDisconnect function as you said( this case initiator leaving room is nothing but ending/completing the meeting bcz its 1:1).

@markandrus I fixed with status call back and REST API.
I am closing it for now.

@ihatecodinglikeking alternatively, I wonder, would you like an API like

remoteParticipant.disconnect()

Hi @markandrus I'd love this API. I wonder if it's implemented somewhere or is it just hypothetical.
Thanks.

@markandrus I fixed with status call back and REST API.
I am closing it for now.

Hey can you share you Rest API ?

Was this page helpful?
0 / 5 - 0 ratings