-1

When user joins a room that has another individual already in the room then the person joining should see the video and audio of the user already in the room*.

As seen in the docs, on room initialization I iterate over all participants in the room to attach to their track by executing participant.tracks.forEach(publication => publication.track.attach()). However I get an error because track is undefined which I learn was because I was not subscribed to the track.

How do I subscribe to tracks/track publication of users who are already in the room?

*The user already in the room is using and old Android SDK and the user in the room is using the latest 2.0 Javascript SDK

simondefreeze
  • 189
  • 16

1 Answers1

2

Twilio developer evangelist here.

When you iterate over those existing participants their tracks may be published but you might not have subscribed to the track yet. So you should check if the track publication isSubscribed yet and only add the track at that point. Otherwise you can listen to the track publication's subscribed event and then attach the track.

room.participants.forEach(participant => {
  participant.tracks.forEach(publication => {
    if (publication.isSubscribed) {
      const mediaElement = publication.track.attach();
      // add mediaElement to the DOM
    } else {
      publication.on("subscribed", track => {
        const mediaElement = track.attach();
        // add mediaElement to the DOM
      })
    }
  })
})

Alternatively, you can also listen to the trackSubscribed event of a participant and perform the same action at that point.

philnash
  • 70,667
  • 10
  • 60
  • 88
  • Hi @philnash, thanks a lot for your help as this solves my problem. If you have access, could you look into updating the "migrating v1 to v2" docs as it does not include that in the examples there whereas the github readme does. The frustrating thing is I resolved it about 10 minute's before reading your post :'D – simondefreeze Aug 09 '21 at 02:32
  • That was not actually a change from v1 to v2, track publications and subscription events arrived before that update. I will look into updating the [room docs you referenced in the question](https://www.twilio.com/docs/video/javascript-getting-started#display-a-remote-participants-video) though. – philnash Aug 09 '21 at 02:34
  • Odd that I just ran into this issue when upgrading then. With v1 I was able to iterate over participants tracks and run track.attach() and that worked regardless of any tracks subscription status. With V2 I can iterate over participant trackPublications but I could not access the track when the app is initialized as it was not subscribed. Potentially the wrong approach was taken in my implementation of V1 and I should have been listening for subscribed events? – simondefreeze Aug 09 '21 at 02:44
  • Ah, I think maybe the events and publications/subscriptions came along before v2, but the split of getting a publication and getting a subscription only came after v2. – philnash Aug 09 '21 at 02:56
  • The app that I upgraded had quite an old implementation of the Twilio SDK so it could have been written pre that split being implemented – simondefreeze Aug 09 '21 at 03:23
  • Ah yes, probably. Glad to hear it's getting upgraded! I would recommend keeping on top of point releases from the Video SDK too, as they either include bug fixes, performance improvements or keep up with browser implementations. – philnash Aug 09 '21 at 04:55