Suggestions

close search

Back to Basic Video Chat Overview

Basic Video Chat Step 3: Initializing an OpenTok session

  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

An OpenTok session connects different clients letting them share audio-video streams and send messages. Clients in the same session can include iOS, Android, and web browsers.

Session ID — Each client that connects to the session needs the session ID, which identifies the session. Think of a session as a room, in which clients meet. Depending on the requirements of your application, you will either reuse the same session (and session ID) repeatedly or generate new session IDs for new groups of clients.

Important: This demo application assumes that only two clients — the local Web client and another client — will connect in the same OpenTok session. For test purposes, you can reuse the same session ID each time two clients connect. However, in a production application, your server-side code must create a unique session ID for each pair of clients. In other applications, you may want to connect many clients in one OpenTok session (for instance, a meeting room) and connect others in another session (another meeting room).

Since this app uses the OpenTok archiving feature to record the session, the session must be set to use the routed media mode, indicating that it will use the OpenTok Media Router. The OpenTok Media Router provides other advanced features (see The OpenTok Media Router and media modes). If your application does not require the features provided by the OpenTok Media Router, you can set the media mode to relayed.

Token — The client also needs a token, which grants them access to the session. Each client is issued a unique token when they connect to the session. Since the user publishes an audio-video stream to the session, the token generated must include the publish role (the default). For more information about tokens, see the OpenTok Token creation overview.

API key — The API key identifies your OpenTok developer account.

Upon starting up, the application application executes the following code in the app.js file.

$(document).ready(function() {
  // Make an Ajax request to get the OpenTok API key, session ID, and token from the server
  $.get(SAMPLE_SERVER_BASE_URL + '/session', function(res) {
    apiKey = res.apiKey;
    sessionId = res.sessionId;
    token = res.token;

    initializeSession();
  });
});

This method makes an XHR (or Ajax request) to the "/session" endpoint of the web service. The web service returns an HTTP response that includes the session ID, the token, and API key formatted as JSON data:

{
     "sessionId": "2_MX40NDQ0MzEyMn5-fn4",
     "apiKey": "12345",
     "token": "T1==cGFydG5lcl9pZD00jg="
}
Got it! Go to step 4
  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