1

I'm subscribing to only one of many publishers, one at a time (video monitor). So, I initially open all subscribers and when the onconnected event is fired, I store each of them in a hash table (stream.connection.data holds ID). I subscribe to the first, then unsubscribe and subscribe to the next. However, I've had to kill the onconnected event handler so they didn't keep firing multiple times: Tokbox streamCreated being called same number of times client is called

Now, when a new participant joins the session late (after the event handler has been removed) I don't know how to subscribe to their stream since no event fires. I know they have connected and are streaming because I get a message from their application that contains the ID of their stream.

So, I need a way to iterate through all the steams in the session, find the new one, and subscribe to it when it's that person's turn. How, can I get all the streams in a session and look at their connection ID's?

Velocedge
  • 1,222
  • 1
  • 11
  • 35

1 Answers1

0

TokBox Developer Evangelist here.

To know the number of streams in a session on the client side, I recommend listening to the following session events:

  • streamCreated - fires when someone starts publishing in a session
  • streamDestroyed - fires when someone stops publishing in a session.

I would create an object like below to keep a record of all streams in a session:

const streams = {};
session.on({
  streamCreated: event => {
    streams[event.stream.streamId] = event.stream;
  },
  streamDestroyed: event => {
    delete streams[event.stream.streamId];
  },
};

This would then allow you to access the stream object and subscribe like so:

const stream = streams['f39c6-ae02-100c-9727-b3bf2']; // please note that this is a random stream Id
const subscriber = session.subscribe(stream);

If you would like to know the number of streams in a session on the server side, you can use Session Monitoring and listen to the same events.

Manik
  • 1,495
  • 11
  • 19
  • I do keep track of them in a hash table based on the streamcreated event. I was able to get this to work just due to an error in my code, but I would still like to know if there is a way to enumerate the streams in a session without first catching them in the event. – Velocedge Jan 01 '19 at 15:35
  • Currently, there is no public API that exposes the active streams in a session, you would have to keep track of the streams based on the events. – Manik Jan 02 '19 at 17:45