Suggestions

close search

Back to Basic Video Chat Overview

Basic Video Chat Step 4: Generating a token

  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
  7. 7
    Basic Video Chat Step 7:
    Customizing the UI

First, set the app to use the web service described in the previous two sections:

In a production application, you will always want to use a web service to obtain a unique token each time a user connects to an OpenTok session.

You will want to authenticate each user (using your own server-side authentication techniques) before sending an OpenTok token. Otherwise, malicious users could call your web service and use tokens, causing streaming minutes to be charged to your OpenTok developer account. Also, it is a best practice to use an HTTPS URL for the web service that returns an OpenTok token, so that it cannot be intercepted and misused.

You can now test the app in the debugger. On successfully connecting to the session, the app logs "Session Connected" to the debug console.

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 iOS 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 calls the [self getSessionCredentials:] method (defined in the ViewController.m file). This method calls a web service that provides an OpenTok session ID, API key, and token to be used by the client. In the Config.h file (see the previous section), set the SAMPLE_SERVER_BASE_URL constant to the base URL of the web service that handles OpenTok-related API calls:

define SAMPLE_SERVER_BASE_URL @"http://YOUR-SERVER-URL/"

The "/session" endpoint of 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="
}

Upon obtaining the session ID, token, and API, the app calls the [self doConnect] method to initialize an OTSession object and connect to the OpenTok session:

- (void)doConnect
{
    // Initialize a new instance of OTSession and begin the connection process.
    _session = [[OTSession alloc] initWithApiKey:_apiKey
                                       sessionId:_sessionId
                                        delegate:self];
    OTError *error = nil;
    [_session connectWithToken:_token error:&error];
    if (error)
    {
        NSLog(@"Unable to connect to session (%@)",
              error.localizedDescription);
    }
}

The OTSession object (_session), defined by the OpenTok iOS SDK, represents the OpenTok session (which connects users).

The [OTSession connectWithToken:error] method connects the iOS app to the OpenTok session. You must connect before sending or receiving audio-video streams in the session (or before interacting with the session in any way).

This app sets self to implement the [OTSessionDelegate] interface to receive session-related messages. These messages are sent when other clients connect to the session, when they send audio-video streams to the session, and upon other session-related events, which we will look at in the following sections.

  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
  7. 7
    Basic Video Chat Step 7:
    Customizing the UI