2
votes

I created online education video portal using opentok . Number of students should see the video of teacher. also teacher will see videos of all connected students. with below code i can prevent to subscribe ourself :-

function subscribeToStreams(streams) {
            for (var i = 0; i < streams.length; i++) {
                // Make sure we don't subscribe to ourself

                if (streams[i].connection.connectionId == session.connection.connectionId) {
                    return;
                }

                //if (streams[i].connection.connectionId != session.connection.connectionId) {
                    // Create the div to put the subscriber element in to
                    var container = document.getElementById('videoContainer');
                    var div = document.createElement('div');

                    var id = 'stream' + streams[i].streamId;
                    div.setAttribute('id', id);
                    container.appendChild(div);

                    // Subscribe to the stream
                    session.subscribe(streams[i], div.id);
                    div.style.width = '20%';
                    div.style.height = '20%';
                    div.style.left = '80%';
                //}
            }
        }

I want to prevent students to see other student videos. student's should be able to see teacher's video only.

help will be greatly appreciated. thanks

1

1 Answers

1
votes

The best way to do this is to use tokens. Assuming you are generating a token for every user (which you should), you have to put data into every user's token by setting the connection_data property. Example in Ruby. I would set the string "student" or "teacher" into the token's connection_data.

On streamCreated event, you can look up the connection_data associated with the stream in your event handler: event.stream.connection.data

On your students side, you can simply check connection data to only subscribe to teacher's stream when you get streamCreated event:

function(e){
  if( e.stream.connection.data == "teacher" ){
    // Create the div to put the subscriber element in to
    var container = document.getElementById('videoContainer');
    var div = document.createElement('div');

    var id = 'stream' + streams[i].streamId;
    div.setAttribute('id', id);
    container.appendChild(div);

    // Subscribe to the stream
    session.subscribe(streams[i], div.id);
  }
}

On the teacher's side, he can simply subscribe to every incoming stream.

Hope that helps,

Good luck!