Are you a student developer? Spend your summer in San Francisco building your startup at TokBox. Apply for IncuBox now.

Five Steps to Get Started with OpenTok

    Get started quickly by building a simple app in less than 15 minutes. Follow the steps below:

  1. 1Get an API key
  2. 2Generate a session & token
  3. 3Initialize & connect to session
  4. 4Publish a stream
  5. 5Subscribe to streams
  6. This tutorial is meant for those who learn by doing. You will build a simple application that publishes and subscribes to video streams. If you prefer, you can instead read the conceptual API overview.

    You can copy the code from this page or download the source code to get started using OpenTok in your own application. Note that this code will only work if it is running from your webserver. If you would like to run this code locally (not on a webserver), you should follow these instructions to configure your Flash permissions.

    If you'd rather see this tutorial in video form, watch the screencast here.

  1. 1Get an API key

    First you need an API key to get started using OpenTok. Fill out the form below to get one on this page (it will also be emailed to you).

    I agree to the TokBox Platform Terms of Service
  2. 2Generate a session & token

    OpenTok revolves around the concept of sessions. Sessions can be thought of as rooms in which video streams are published and subscribed to.

    Users connect to sessions using tokens. Tokens determine what permissions a user has (i.e. moderator, publisher, or subscriber).

    In a production environment, typically you would generate a session and token on your server using the Server-Side SDK. For development purposes we will skip this process and generate a static session and use the special token 'devtoken' to speed things along. Press the button here to generate a session.

  3. 3Initialize & connect to session

    Now that we have an API key and session ID to connect to, we can initialize and connect to an OpenTok session.

    The code for accomplishing this is segmented in to three parts. First, we import the OpenTok JavaScript API (line 4). Second, we set the API key, sessionId, and token variables for the session (lines 7-9), which have been populated with the ones you generated. Last, we initialize the session, set up an event handler to alert us when we connect to the session, and then connect to the session (lines 13-19).

    Execute the code to connect to your session. You will received an alert when it's connected.

    			
    			<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    			<head>
    				<title>OpenTok Getting Started</title>
    				<script src="http://staging.tokbox.com/v0.91/js/TB.min.js"></script>
    
    				<script type="text/javascript">
    					var apiKey = '1127';
    					var sessionId = '14685d1ac5907f4a2814fed28294d3f797f34955';
    					var token = 'devtoken';						
    
    					TB.setLogLevel(TB.DEBUG); // Set this for helpful debugging messages in console
    
    					var session = TB.initSession(sessionId);			
    					session.addEventListener('sessionConnected', sessionConnectedHandler);			
    					session.connect(apiKey, token);
    
    					function sessionConnectedHandler(event) {
    						alert('Hello world. I am connected to OpenTok :).');
    					}			
    				</script>
    
    			</head>
    
    			<body>
    			</body>
    			</html>
  4. 4Publish a stream

    Once we're connected to the session publishing a stream to the session is simple, just call session.publish() and pass it the name of the div to replace with your video stream (line 21).

    Execute the code to connect to your session and publish your stream.

    
    			<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    			<head>
    				<title>OpenTok Getting Started</title>
    				<script src="http://staging.tokbox.com/v0.91/js/TB.min.js"></script>
    
    				<script type="text/javascript">
    					var apiKey = '1127';
    					var sessionId = '14685d1ac5907f4a2814fed28294d3f797f34955';
    					var token = 'devtoken';						
    					
    					TB.setLogLevel(TB.DEBUG);			
    
    					var session = TB.initSession(sessionId);			
    					session.addEventListener('sessionConnected', sessionConnectedHandler);
    					session.connect(apiKey, token);
    
    					var publisher;
    
    					function sessionConnectedHandler(event) {
    						// Publish my webcam stream and put it in a div
    						publisher = session.publish('myPublisherDiv');
    					}
    				</script>
    
    			</head>
    
    			<body>
    				
    </body> </html>
  5. 5Subscribe to streams

    Subscribing to a stream is also simple, just call session.subscribe() and pass it the stream object and the id of the div the stream should replace (line 45).

    In this app we subscribe to all streams in the session (though you could add logic here to suit your own application). To do this, we call our subscribeToStreams function within the sessionConnected and streamCreated event handlers, which are executed when you connect to the session and when a new stream is created.

    Execute the code to publish and subscribe to streams in the session.

    
    			<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
    			<head>
    				<title>OpenTok Getting Started</title>
    				<script src="http://staging.tokbox.com/v0.91/js/TB.min.js"></script>
    
    				<script type="text/javascript">
    					var apiKey = '1127';
    					var sessionId = '14685d1ac5907f4a2814fed28294d3f797f34955';
    					var token = 'devtoken';						
    					
    					TB.setLogLevel(TB.DEBUG);			
    
    					var session = TB.initSession(sessionId);			
    					session.addEventListener('sessionConnected', sessionConnectedHandler);
    					session.addEventListener('streamCreated', streamCreatedHandler);			
    					session.connect(apiKey, token);
    
    					var publisher;
    
    					function sessionConnectedHandler(event) {
    						publisher = session.publish('myPublisherDiv');
    						
    						// 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);
    						}
    					}
    				</script>
    			</head>
    
    			<body>
    				
    </body> </html>

Next Steps

Congrats! You've built your first OpenTok app (hopefully in less than 15 minutes).

If you'd like to learn more, we suggest that you either read our conceptual overview or dive in to more advanced tutorials to learn how to do things like resizing, moderation, and audio control. If you have questions please post them in our forums.

Launch Your App

When you're done building your app, fill out the launch form to get access to our production servers for improved performance. Learn how to move your application from the staging servers (used in this tutorial) to production here.