OpenTokSDK for node.js
JavaScript CoffeeScript
Switch branches/tags
Nothing to show
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.

OpenTokSDK for Node.js

OpenTok is a free set of APIs from TokBox that enables websites to weave live group video communication into their online experience. With OpenTok you have the freedom and flexibility to create the most engaging web experience for your users. OpenTok is currently available as a JavaScript and ActionScript 3.0 library. Check out and for more information.

This is the OpenTok NodeJS Module.


To install using npm, add OpenTok to package.json and run npm install:

"dependencies" : {  
    "opentok" : "0.3.x",  

To install as a regular npm package just type npm install opentok

How to use

API key and API secret

Request your API key and API secret at


In order to use any of the server side functions, you must first create an OpenTokSDK object with your developer credentials.
You must pass in your API key and API secret.

var key = '';    // Replace with your API key  
var secret = '';  // Replace with your API secret  
var opentok = new OpenTok.OpenTokSDK(key, secret);

Creating Sessions

Use your OpenTokSDK object to create a session_id. See for more details. createSession takes 2-3 parameters:

location [string] - give Opentok a hint on where you are running your application by specifiying an IP (e.g. '')
properties [object] - OPTIONAL. Set peer to peer as enabled or disabled
callback [fn(sessionId)] - This is a function that handles the server response after session has been created. The result sessionId is a string.

Example: P2P disabled (default)

var location = ''; // use an IP or 'localhost'
var sessionId = '';
opentok.createSession(location, function(result){
  sessionId = result;
  // Do things with sessionId

Example: P2P enabled

var location = ''; // use an IP of 'localhost'
var sessionId = '';
opentok.createSession(location, {'p2p.preference':'enabled'}, function(result){
  sessionId = result;

Generating Token

With the generated session_id and an OpenTokSDK object, you can start generating tokens for each user. See for more details. generateToken takes in an object with 1-4 properties, and RETURNS a token as a string:

session_id [string] - REQUIRED. This token is tied to the session it is generated with
role [string] - OPTIONAL. opentok.RoleConstants.{SUBSCRIBER|PUBLISHER|MODERATOR}. Publisher role used when omitted. expire_time [int] - OPTIONAL. Time when token will expire in unix timestamp. connection_data [string] - OPTIONAL. Stores static metadata to pass to other users connected to the session. (eg. names, user id, etc)


var token = opentok.generateToken({session_id:session_id, role:OpenTok.RoleConstants.PUBLISHER, connection_data:"userId:42"});

Downloading Archive Videos

To Download archived video, you must have an Archive ID (from the client), and a moderator token. For more information see

Quick Overview of the javascript library:

  1. Create an event listener on archiveCreated event: session.addEventListener('archiveCreated', archiveCreatedHandler);
  2. Create an archive: archive = session.createArchive(...);
  3. When archive is successfully created archiveCreatedHandler would be triggered. An Archive object containing archiveId property is passed into your function. Save this in your database, this archiveId is what you use to reference the archive for playbacks and download videos
  4. After your archive has been created, you can start recording videos into it by calling session.startRecording(archive)
    Optionally, you can also use the standalone archiving, which means that each archive would have only 1 video:

Get Archive Manifest

With your moderator token and a OpenTokSDK object, you can generate a OpenTokArchive object, which contains information for all videos in the Archive
OpenTokSDK.getArchiveManifest() takes in 3 parameters: archiveId and moderator token, and a callback function

archive_id [string] - REQUIRED. Get this from the client that created the archive.
token [string] - REQUIRED. Get this from the client or the generate_token method.
handler [fn(tbarchive)] - REQUIRED. This function is triggered after it receives the Archive Manifest. The parameter is an OpenTokArchive object. The resources property of this object is array of OpenTokArchiveVideoResource objects, and each OpenTokArchiveVideoResource object represents a video in the archive.

Example: (opentok is an OpentokSDK object)

var token = 'moderator_token';
var archiveId = '5f74aee5-ab3f-421b-b124-ed2a698ee939'; // Obtained from Javascript Library

opentok.getArchiveManifest(archiveId, token, function(tbarchive){
  var otArchive = tbarchive;

Get video ID

OpenTokArchive.resources is an array of OpenTokArchiveVideoResource objects. OpenTokArchiveVideoResource has a getId() method that returns the videoId as a string.


opentok.getArchiveManifest(archiveId, token, function(tbarchive){
  var vidID = tbarchive.resources[0].getId();

Get Download URL

OpenTokArchive objects have a downloadArchiveURL(video_id, handler) method that will return a URL string for downloading the video in the archive. Video files are FLV format.

video_id [string] - REQUIRED. The Video ID returned from OpenTokArchiveVideoResource.getId()
handler [fn(url)] - REQUIRED. This function is triggered after it receives the URL for video. The result is a URL string.


var url = '';
otArchive.downloadArchiveURL(vidID, function(resp){
  url = resp;


Check out the basic working example in examples/app.js

Want to contribute?

To run test suite:

jasmine-node --coffee spec/