Share this URL:

or open in a new tab.

It's this easy...

Build our demo app in 25 lines of code.

// Initialize API key, session, and token...
// Think of a session as a room, and a token as the key to get in to the room
// Sessions and tokens are generated on your server and passed down to the client
var apiKey = "1127";
var sessionId = "1_MX4xMTI3fn5XZWQgTWF5IDIyIDE4OjM3OjA4IFBEVCAyMDEzfjAuODQ5Mzg0OH4";
var token = "T1==cGFydG5lcl9pZD0xMTI3JnNpZz04NDMwMGMwZjQ3MjRjYzcwMTQzMjVjZjY0ZjIxZGMyMjkwZWJlNmYxOnNlc3Npb25faWQ9MV9NWDR4TVRJM2ZuNVhaV1FnVFdGNUlESXlJREU0T2pNM09qQTRJRkJFVkNBeU1ERXpmakF1T0RRNU16ZzBPSDQmY3JlYXRlX3RpbWU9MTM2OTI3MzAyOCZub25jZT03ODAyMjgmcm9sZT1wdWJsaXNoZXI=";

// Enable console logs for debugging
TB.setLogLevel(TB.DEBUG);

// Initialize session, set up event listeners, and connect
var session = TB.initSession(sessionId);
session.addEventListener('sessionConnected', sessionConnectedHandler);
session.addEventListener('streamCreated', streamCreatedHandler);
session.connect(apiKey, token);
function sessionConnectedHandler(event) {
  var publisher = TB.initPublisher(apiKey, 'myPublisherDiv');
  session.publish(publisher);

  // Subscribe to streams that were in the session when we connected
  subscribeToStreams(event.streams);
}

function streamCreatedHandler(event) {
  // Subscribe to any new streams that are created
  subscribeToStreams(event.streams);
}

function subscribeToStreams(streams) {
  for (var i = 0; i < streams.length; i++) {
    // Make sure we don't subscribe to ourself
    if (streams[i].connection.connectionId == session.connection.connectionId) {
      return;
    }

    // Create the div to put the subscriber element in to
    var div = document.createElement('div');
    div.setAttribute('id', 'stream' + streams[i].streamId);
    document.body.appendChild(div);

    // Subscribe to the stream
    session.subscribe(streams[i], div.id);
  }
}

IRC Live Chat

Have a quick question? Chat with TokBox Support on IRC. Join chat