TB class
The first step in using the TokBox API is to call the TB.initSession() method. Other methods of TB object
check for system requirements, initialize a DeviceManager object, and set up error logging.
Note that all of the methods of the TB class are static methods.
TB methods
The TB object includes the following static methods:
| Method | Description |
|---|---|
| TB.addEventListener(eventType:String, listener:Function) | Registers a method as an event listener for a specific event. |
| TB.checkSystemRequirements():Number | Checks the system for OpenTok API support. Returns 1 if the system supports the OpenTok API; 0 if it does not. |
| TB.initDeviceManager(apiKey:String):DeviceManager | Initializes and returns a DeviceManager. |
|
TB.initRecorderManager(apiKey:String):RecorderManager
|
Initializes and returns a RecorderManager. |
| TB.initSession(sessionId:String):Session | Initializes and returns the local session object for a specified session ID. |
| TB.log(message:String) | Sends a string to the log output. |
| TB.removeEventListener(eventType:String, listener:Function) | Removes an event listener for a specific event. |
| TB.setLogLevel(logLevel:Number) | Sets the API log level. |
| TB.updateSystemRequirements() | Provides an interface for the user to update to the latest version of Flash Player. |
TB.addEventListener(type:String, listener:Function)
Registers a method as an event listener for a specific event. The TB class can dispatch one type of event an exception event. See TB events.
Parameters
type (String) This string identifying the type of event. The TB object
can only dispatch one type of event: an exception event. This event is defined by the ExceptionEvent class,
and it is of type "exception". The ExceptionEvent object defines this event.
listener (Function) The function to be invoked when the TB object dispatches the event.
Example
The following example registers a function, exceptionHandler() as the event handler
for exception events.
TB.addEventListener("exception", exceptionHandler);
function exceptionHandler(event) {
alert("Exception:" + event.title + ". " + event.message);
}
TB.checkSystemRequirements():Number
Checks if the system supports the OpenTok API.
Returns
Returns a number: 1 if the system supports the OpenTok API; 0 if it does not.
TB.initDeviceManager(apiKey:String):DeviceManager
Initializes and returns a DeviceManager object.
Parameters
apiKey (String) The API key that TokBox provided you when you registered for the OpenTok API.
Returns
DeviceManager A DeviceManager object, with which you can manage cameras and microphones for published streams.
See
TB.initRecorderManager(apiKey:String):RecorderManager
Initializes and returns a RecorderManager object.
Parameters
apiKey (String) The API key that TokBox provided you when you registered for the OpenTok API.
Returns
RecorderManager A RecorderManager object, with which you can record and play back stand-alone archives.
See
TB.initSession(sessionId:String):Session
Initializes and returns the local session object for a specified session ID.
The TB.initSession() method performs setup local to
the browser and readies the web page for connecting to the session.
You connect to the session using the connect() method
of the Session object returned by the TB.initSession() method.
Note that calling TB.initSession() does not initiate communications
with the cloud. It simply initializes the Session object that you can use to
connect (and to perform other operations once connected).
Parameters
sessionId (String) Session ID generated by TokBox that represents the session to which you are planning to connect.
Returns
Session The session object through which all further interactions with the session will occur.
Example
The following code initializes a session and sets up an event listener for when the session connects:
var sessionID = "123456789abcdef0123456789abcdef012345678"; // replace with your session ID
var apiKey = 1234; // replace with your API key
var token = "devtoken"; // replace with your connection token
var session = TB.initSession(sessionID);
session.addEventListener("sessionConnected", sessionConnectHandler);
session.connect(apiKey, token);
function sessionConnectHandler(sessionConnectEvent) {
//
}
The event listener for the sessionConnect event can process the SessionConnectEvent object.
(In this example, the sessionConnectHandler() function is the event listener.) For instance,
it can loop through the array of the current streams in the session and present them on the page.
TB.log(message:String)
Sends a string to the log output:
- The string is appended to a DOM element that has the ID "opentok_console", if such an element exists.
- The string is sent to debugger console (such as Firebug), if one exists.
Parameters
message (String) The string to log.
TB.removeEventListener(type:String, listener:Function)
Removes an event listener for a specific event.
Parameters
type (String) The string identifying the type of event.
listener (Function) The event listener function to remove.
The TB object can only dispatch one type of event an exception event.
The TB object throws an exception if the listener name is invalid.
TB.setLogLevel(logLevel:String)
Sets the API log level.
Calling TB.setLogLevel() sets the log level for runtime log messages that are the OpenTok library generates. The default value for the log level is TB.NONE.
The OpenTok JavaScript library displays log messages in two places:
- Appended to a DOM element that has the ID "opentok_console", if such an element exists
- A debugger console (such as Firebug), if one exists
Parameters
logLevel (Number) The degree of logging desired by the developer:
-
TB.NONE API logging is disabled. -
TB.ERROR Logging of errors only. -
TB.WARN Logging of warnings and errors. -
TB.INFO Logging of other useful information, in addition to warnings and errors. -
TB.DEBUG Fine-grained logging of all API actions.
Example
The following example logs an error message in the <opentok_console> div element. The error is caused because the code attempts to publish a stream before the Session object dispatches a sessionConnected event.
<script>
mySession = TB.initSession(sessionId);
mySession.publish("publishContainer");
</script>
<body>
<div id="publish_container" />
<div id="opentok_console" />
</body>
TB.updateSystemRequirements()
Provides an interface for the user to update to the latest version of Flash Player.
Examples
The following code alerts the user to upgrade if they do not have a version of Flash Player that supports OpenTok:
if (!TB.checkSystemRequirements()) {
TB.upgradeSystemRequirements();
}
The following code alerts the user to upgrade if they do not have a version of Flash Player that supports publishing H.264, which is supported in Flash Player 11:
if (!swfobject.hasFlashPlayerVersion('11')) {
TB.upgradeSystemRequirements();
}
(The swfobject.hasFlashPlayerVersion() method is included in the TB.js file.)
Once a client has connected to a session, you can check the session.capablities.publishH264
property to determine if the user can publish h.264 video. You can then notify the user to
update if they do not have the supported Flash Player version:
var session = TB.initSession(SESSION_ID);
session.addEventListener("sessionConnected", sessionConnectedHandler);
session.connect(API_KEY, TOKEN);
function sessionConnectedHandler(event) {
if (!session.capabilities.publishH264) {
TB.upgradeSystemRequirements();
}
}
TB events
The TB class can dispatch events of the following type:
| Event type | Event class | Description |
|---|---|---|
"exception"
|
ExceptionEvent | The app encounters an asynchronous exception. |
IRC Live Chat
Have a quick question? Chat with other developers. Join chat
TokBox staff may not be online right now. To reach them during off-hours, visit the forums.