skip to Main Content

In my twillio application i’m getting random errors when tryin to disconnect from the room.

This is the code I’m trying to execute upon exit:

        this.log('Leaving room...');
        try {
            this.activeRoom ? this.activeRoom.disconnect().catch(console.log) : console.log('No active room to disconnect / check disconnect logic');
        } catch (error) {
            console.log(error)
        }

this creates the following error:

Error

I’m not so concerned about the error itself but the fact that i cannot catch the error.
As you can see both try/catch block and .catch() were added but I’m still getting this error.

UPDATE_1

After digging thru the api I found and implemented this:

this.activeRoom.on('disconnected', function(room, error) {
  if (error) {
    console.log('Unexpectedly disconnected:', error);
  }
  myRoom.localParticipant.tracks.forEach(function(track) {
    track.stop();
    track.detach();
  });
});

however this error still comes up for me.

UPDATE_2

After implementing solution proposed in an answer this is what I got(still the same error):
enter image description here

3

Answers


  1. Twilio developer evangelist here.

    That is weird that you can’t seem to catch the error. I’m not sure I have an answer for that.

    However, you could check whether the local participant is connected before trying to disconnect them. A Participant object has a state property that can be “connected”, “disconnected” or “failed”.

    So, you could:

    if (this.activeRoom && this.activeRoom.localParticipant.state === 'connected') {
      this.activeRoom.disconnect();
    }
    
    Login or Signup to reply.
  2. For anyone that runs into this issue. The stack trace you get from room.disconnect() is misleading.

    The actual error is in your room.localParticipant.publishTrack(track) call. That function returns a promise, and will resolve (with an exception) the moment you call room.disconnect().

    If you haven’t handled failure there, you’ll get the error shown in this question. So just add a .catch() statement to all your publishTrack calls.

    Login or Signup to reply.
  3. I use this to solve this issue

    (new Promise(() => room.disconnect())).then(() => {}).catch(() => {})
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search