Suggestions

close search

Back to Basic Video Chat Overview

Basic Video Chat Step 6: Subscribing to audio-video streams

  1. 1
    Basic Video Chat Step 1:
    Setting up the test web service
  2. 2
    Basic Video Chat Step 2:
    Configuring and testing the app
  3. 3
    Basic Video Chat Step 3:
    Initializing an OpenTok session
  4. 4
    Basic Video Chat Step 4:
    Connecting to the session
  5. 5
    Basic Video Chat Step 5:
    Publishing a stream to the session
  6. 6
    Basic Video Chat Step 6:
    Subscribing to audio-video streams

The Session object dispatches a streamCreated event when a new stream (other than your own) is created in a session. A stream is created when a client publishes to the session. The streamCreated event is also dispatched for each existing stream in the session when you first connect. This event is defined by the StreamEvent object, which has a stream property, representing stream that was created. The application adds an event listener for the streamCreated event and subscribes to all streams created in the session using the Session.subscribe() method:

// Subscribe to a newly created stream
session.on('streamCreated', function(event) {
  var subscriberOptions = {
    insertMode: 'append',
    width: '100%',
    height: '100%'
  };
  session.subscribe(event.stream, 'subscriber', subscriberOptions, function(error) {
    if (error) {
      console.log('There was an error publishing: ', error.name, error.message);
    }
  });
});

The Session.subscribe() method takes four parameters:

Got it! I built basic video chat!
  1. 1
    Basic Video Chat Step 1:
    Setting up the test web service
  2. 2
    Basic Video Chat Step 2:
    Configuring and testing the app
  3. 3
    Basic Video Chat Step 3:
    Initializing an OpenTok session
  4. 4
    Basic Video Chat Step 4:
    Connecting to the session
  5. 5
    Basic Video Chat Step 5:
    Publishing a stream to the session
  6. 6
    Basic Video Chat Step 6:
    Subscribing to audio-video streams