JavaScript

Initializing the Subscriber

Finally, we want clients to be able to subscribe to (or view) each other's streams in the session.

  1. In your current app.js file, you should have a comment that says // Subscribe to a newly created stream. Copy the following code and add it directly under that comment:
session.on('streamCreated', function(event) {
  session.subscribe(event.stream, 'subscriber', {
    insertMode: 'append',
    width: '100%',
    height: '100%'
  }, handleError);
});

session.subscribe() is a general method that allows our application to listen for different actions that happen in our Video application's lifecycle. The SDK automatically handles listening (as well as publishing) events on our behalf.

We will listen for the streamCreated event, which signifies that someone else has connected to the stream and has published either video or audio.

session.subscribe() allows us to pass in the stream to subscribe to (event.stream), a div to place them into in our HTML file(subscriber), and some configuration. The SDK will then have the new streams appear on the screen.