Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Created May 4, 2013 14:21
Show Gist options
  • Save JohnMurray/5517635 to your computer and use it in GitHub Desktop.
Save JohnMurray/5517635 to your computer and use it in GitHub Desktop.
// 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_MX4xMTI3fn5TYXQgTWF5IDA0IDA3OjIwOjAzIFBEVCAyMDEzfjAuNDQwODI4MTR-";
var token = "T1==cGFydG5lcl9pZD0xMTI3JnNpZz1jZjRhYzc3OWI5MGNkMzBkMGUyOGZmN2UxMTRjZDlmNDAxY2FiN2VlOnNlc3Npb25faWQ9MV9NWDR4TVRJM2ZuNVRZWFFnVFdGNUlEQTBJREEzT2pJd09qQXpJRkJFVkNBeU1ERXpmakF1TkRRd09ESTRNVFItJmNyZWF0ZV90aW1lPTEzNjc2NzcyMDMmbm9uY2U9MTk1MDcyJnJvbGU9cHVibGlzaGVy";
// 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);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment