OpenTok Developer Guide

In these pandemic situations nowadays, video calling is now making interaction easy. Such video call integration platforms like OpenTok make development easy. Let’s make OpenTok easier to understand and start with some basics of OpenTok and overview, continuing with will go through the authentication process and connecting and disconnecting sessions.

What is OpenTok?

OpenTok is a platform that provides API to stream live video calling functionality into our web application. It allows many features to integrate into web applications like screen sharing, text chat, and many more.

Overview of OpenTok

opentok_overview

Why to choose OpenTok? 

The best part of the OpenTok platform is it doesn’t store data into their servers. They provide an archive API to store the streaming data into their server or path which we provide. All data is encrypted while streaming, which is the best security they provide, even if we use public hotspots.

What are the encryption algorithms and strength of the keys being used?

– OpenTok WebRTC-compatible endpoints use the AES cipher with 128-bit keys to encrypt audio and video, and HMAC-SHA1 to verify data integrity.

– End to end encryption and serve content using HTTPS URLs (All API are secured with secured layers https)

What keys are being used for the encryption?

The endpoints generate random keys at the beginning of the session and in addition, they change periodically during the conversation to make it even safer.

Simple and easy to understand

– OpenTok is easy to understand and implement and available for all platforms

– Only needs to understand the concept of subscriber and publisher, Yes you are ready to call now.

Refer to this link to know more about OpenTok encryption and data security.

How does OpenTok support HIPAA compliance or PHI?

HIPAA requires a secure channel between the communication of patients and doctors, but As I mentioned earlier OpenTok does not store data into their servers unless we set options as an archive while connecting or calling archive API. Because encryption data security is maintained, secures data transmission between patient and doctor. 
“Wide range of healthcare companies who have elected to build applications utilizing the OpenTok platform”.

Alerts and controls provided by OpenTok related to connections

-Limit the maximum number of users in a session.

-Display subscribers count.

-Set up Moderator permissions to force a disconnect.

-Allocate Subscriber-only permissions for those that do not need to publish.

How OpenTok gives best results

There are many companies such as — PubNub, Sinch, Quickblox, and Twilio whose source code can be integrated with third-party APIs like — WebRTC, Restful, and JavaScript, etc. But, OpenTok 
  • Supports multiple platforms like IOS, web, android.
  • Provides SDK for all platforms.
  • Gives the best security for all streams connected.
  • Provides REST API for integration.
  • Charges apply on subscribing to the call it means when the call is connected.
  • Provides features and functionality to set the video call quality, volume and many more
  • A very important part of video calling is auto-connect when the internet is off, due to many reasons video calling can be interrupted.

Things to Be Noted

opentok-platform

How to start with OpenTok?

authentication-process-steps

Steps to Generate 

  1. Create your account. Go to https://tokbox.com/account -> create a project
  2. Get AP I and Secret Key to connect with the OpenTok API.
  3. Authenticate your API and Secret Key using JWT.
Payload
{
    "iss": "12345678",
    "ist": "project",
    "iat": 1588667570,
    "exp": 1588900001
}

iss is project API key.

ist can be “project” or “account” for session creation project is passed

iat is current epoch timestamp in seconds

exp is expiration time will be maximum 5 mins

Generates JWT token
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiI0NjcyMDI3MiIsImlhdCI6MTU4ODY4MzczNywiZXhwIjoxNT…
Generating tokens in JAVA
import com.OpenTok.TokenOptions;
import com.OpenTok.Role;

// Generate a token from just a sessionId (fetched from a database)

String token = OpenTok.generateToken(sessionId);

// Generate a token by calling the method on the Session (returned from createSession)

String token = session.generateToken();

// Set some options in a token
String token = session.generateToken(new TokenOptions.Builder()
  .role(Role.MODERATOR)

.expireTime((System.currentTimeMillis() / 1000L) + (7 * 24 * 60 * 60)) // in one week
  .data("name=Johnny")
  .build());

4. Create a session and get your sessionId

Create session 
REST API calls must be authenticated using a custom HTTP header — X-OpenTok-AUTH — along with a JSON web token

Type: POST

Headers :  

headers: { ‘X-OpenTok-AUTH’:’Token’, ‘Content-Type’: ‘application/json’ },

Pass params : 

  1. location: IP address
  2. ArchiveMode: always (to archive the session automatically by default it’s manual, we can archive session by calling REST /archive POST method). 
  3. p2p.preference: disabled (if archiveMode is always)

Response 

[
    {
        "properties": null,
        "session_id":        "1_MX40NjcyMDI3Mn5-MTU4ODY4Mzc3MDYyNn5jRmRCcjk2cy9Ndk1MOFBVQzlKYjZHUHF-fg",
        "project_id": "12345678", // api key
        "partner_id": "12345678",
        "create_dt": "Tue May 05 06:02:50 PDT 2020",
        "session_status": null,
        "status_invalid": null,
        "media_server_hostname": null,
        "messaging_server_url": null,
        "messaging_url": null,
        "symphony_address": null,
        "ice_server": null,
        "session_segment_id": "2736846a-cf56-417f-9d94-6116b07fd71a",
        "ice_servers": null,
        "ice_credential_expiration": 86100
    }
]

5. Init session – session initialization will allow to publish and subscribe the session

// Replace with your OpenTok API key and session ID:

var session = OT.initSession(apiKey, sessionID);

6. Create connection using connect function (Use JWT token)

session.connect(token, function(error) {
  if (error) {
    console.log("Error connecting: ", error.name, error.message);
  } else {
    console.log("Connected to the session.");
  }
});

7. The session can be disconnected due to an any error, find the error by listening to the sessionDisconnected event and find the reason using “event.reason”

session.on({
  sessionDisconnected: function(event) {
    if (event.reason === 'networkDisconnected') {
      showMessage('You lost your internet connection.'
        + 'Please check your connection and try connecting again.');
    }
  }
});
Now we have done with creating a connection. I hope you like it and surely help you out to start with OpenTok, we will learn more about how to start with video calling and how chat applications can be built using OpenTok.

subscribe to our newsletter

   
   

About Author

devashree_hingne

Devashree Hingne

Devashree is a Full-stack developer with around 2 years of experience. Her expertise is in the angular framework and has worked on angular 4+ versions. She has also worked on nodeJS and Ionic framework version 3.
Related Posts

Leave a Comment

Integrating-SendGrid-with-Django-rest-framework