Permalink
Browse files

Merge pull request #5 from songz/master

Updated README for clarification.
  • Loading branch information...
2 parents 43eff62 + 7c2e433 commit 72f18eedbd468e0087985ab91e140cfda9e879b6 @bsstoner committed May 28, 2012
Showing with 288 additions and 27 deletions.
  1. +129 −12 README.md
  2. +97 −9 lib/opentok.js
  3. +62 −6 spec/opentok_spec.coffee
View
141 README.md
@@ -1,25 +1,142 @@
-# OpenTokSDK for node.js
+# OpenTokSDK for Node.js
- node.js version of OpenTok's Server Side SDK, ported from their
-Python/Ruby versions.
+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 <http://www.tokbox.com/> and <http://www.tokbox.com/opentok/tools/js/gettingstarted> for more information.
-## Install
+This is the official OpenTok NodeJS Module.
- npm install opentok
+## Installation
-## Example
+To install using npm, add OpenTok to `package.json` and run `npm install`:
+<pre>
+"dependencies" : {
+ "opentok" : ">= 0.2.0",
+ ...
+}
+</pre>
- Check out the basic working example in examples/app.js
+To install as a regular npm package just type `npm install opentok`
+
+## How to use
+
+### API-key and secret
+
+Request your api-key and secret at <http://www.tokbox.com/opentok/tools/js/apikey>. You can use the staging environment for testing. The gem uses this staging environment by default.
+
+### OpenTokSDK
+
+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 *Key* and *Secret*. If your app is in production, you must also pass in a object containing `API_URL` property
+For more information about production apps, check out <http://www.tokbox.com/opentok/api/tools/js/launch>
+
+Example: ( Staging )
+<pre>
+key = '' # Replace with your key
+secret = '' # Replace with your secret
+opentok = new OpenTok.OpenTokSDK(key, secret)
+</pre>
+
+Example: ( Production )
+<pre>
+opentok = new OpenTok.OpenTokSDK(key, secret, {API_URL:'https://api.opentok.com/hl'})
+</pre>
+
+### Creating Sessions
+Use your `OpenTokSDK` object to create `session_id`
+`create_session` takes 2-3 parameters:
+> location (string) - give Opentok a hint on where you are running your application
+> properties (object) - OPTIONAL. Set peer to peer as `enabled` or `disabled`
+> callback (sessionId) - This is a function that handles the server response after session has been created
+
+Example: P2P disabled by default
+<pre>
+location = 'localhost'
+sessionId = ''
+opentok.create_session(location, function(result){
+ sessionId = result;
+});
+</pre>
+
+Example: P2P enabled
+<pre>
+location = 'localhost'
+sessionId = ''
+opentok.create_session(location, {'p2p.preference':'enabled'}, function(result){
+ sessionId = result;
+});
+</pre>
+
+### Generating Token
+With the generated session_id and an OpenTokSDK object, you can start generating tokens for each user.
+`generate_token` takes in an object with 1-4 properties, and RETURNS a token:
+> session_id (string) - REQUIRED. This token is tied to the session it is generated with
+> role (string) - OPTIONAL. subscriber, publisher, or moderator
+> expire_time (int) - OPTIONAL. Time when token will expire in unix timestamp
+> connection_data (string) - OPTIONAL. Stores metadata to pass to other users connected to the session. (names, user id, etc)
+
+Example:
+<pre>
+token = opentok.generate_token({sessionId:session_id, role:OpenTok.RoleConstants.PUBLISHER, connection_data:"hello"})
+</pre>
+### Downloading Archive Videos
+To Download archived video, you must have an Archive ID which you get from the javascript library
-## Questions?
+#### Quick Overview of the javascript library: <http://www.tokbox.com/opentok/api/tools/js/documentation/api/Session.html#createArchive>
+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: <http://www.tokbox.com/opentok/api/tools/js/documentation/api/RecorderManager.html>
- OpenTok Docs:
- http://www.tokbox.com/opentok/tools/js/gettingstarted
+### Get Archive Manifest
+With your **moderator token** and OpentokSDK Object, you can generate OpenTokArchive Object, which contains information for all videos in the Archive
+`get_archive_manifest()` takes in 3 parameters: **archiveId** and **moderator token**, and a handler function
+> archive_id (string) - REQUIRED.
+> token - REQUIRED.
+> handler (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:(Make sure you have the OpentokSDK Object)
+<pre>
+token = 'moderator_token'
+archiveId = '5f74aee5-ab3f-421b-b124-ed2a698ee939' #Obtained from Javascript Library
+
+opentok.get_archive_manifest(archiveId, token, function(tbarchive){
+ otArchive = tbarchive
+});
+</pre>
+
+### Get video ID
+`OpenTokArchive.resources` is an array of `OpenTokArchiveVideoResource` objects. OpenTokArchiveVideoResource has `getId()` method that returns the videoId
+`getId()` will return the video ID (a String)
+
+Example:
+<pre>
+opentok.get_archive_manifest(archiveId, token, function(tbarchive){
+ vid1 = tbarchive.resources[0].getId()
+});
+</pre>
+
+### Get Download Url
+`OpenTokArchive` has `downloadArchiveURL` that will return an url string for downloading the video in the archive. Video files are FLV format
+> video_id (string) - REQUIRED
+> handler (url) - REQUIRED. This function is triggered after it receives the url for video. The parameter is an url string
+
+Example:
+<pre>
+url = "";
+otArchive.downloadArchiveURL(vid, function(resp){
+ url = resp;
+});
+</pre>
+
+
+## Example
+
+ Check out the basic working example in examples/app.js
- OpenTok Dev Mailing List:
- http://groups.google.com/group/opentok-developers
## Want to contribute?
### To run test suite:
jasmine-node --coffee --autotest spec/
+
+
View
@@ -7,7 +7,9 @@ var https = require('https')
, SDK_VERSION = "tbjs-0.91.2011-03-16"
, STAGING_HOST = "staging.tokbox.com"
, PROD_HOST = "api.opentok.com"
- , SESSION_API_ENDPOINT = "/hl/session/create";
+ , SESSION_API_ENDPOINT = "/hl/session/create"
+ , GET_MANIFEST = "/archive/getmanifest/"
+ , GET_URL = "/hl/archive/url/";
// Session Properties definition:
var SessionProperties = exports.SessionProperties = function(){
@@ -20,12 +22,10 @@ var SessionProperties = exports.SessionProperties = function(){
}
}
-var RoleConstants = exports.Roles = function(){
- return {
- 'SUBSCRIBER': "subscriber",
- 'PUBLISHER': "publisher",
- 'MODERATOR': "moderator"
- }
+var RoleConstants = exports.RoleConstants = {
+ SUBSCRIBER: "subscriber",
+ PUBLISHER: "publisher",
+ MODERATOR: "moderator"
}
// Just the session
@@ -57,7 +57,88 @@ OpenTokSDK.prototype.setEnvironment = function(host){
}
}
-OpenTokSDK.prototype.generateToken = function(ops){
+OpenTokSDK.OpenTokArchive = function(sdkObject){
+ var self = this;
+ this.resources = [];
+ this.addVideoResource = function(res){
+ self.resources.push(res);
+ };
+ this.downloadArchiveURL=function(vid, callback){
+ options = {
+ host: sdkObject.api_url,
+ path: GET_URL+sdkObject.archiveId+"/"+vid,
+ method: 'GET',
+ headers: {
+ 'x-tb-token-auth':sdkObject.token
+ }
+ };
+ req = https.request(options, function(res){
+ var chunks = '';
+ res.setEncoding('utf8')
+ res.on('data', function(chunk){
+ chunks += chunk
+ })
+ res.on('end', function(){
+ callback(chunks);
+ })
+ })
+ req.end()
+ };
+};
+OpenTokSDK.OpenTokArchiveVideoResource = function(vid,length,name){
+ this.vid = vid;
+ this.length = length;
+ this.name = name;
+ this.getId = function(){
+ return vid;
+ };
+};
+
+OpenTokSDK.prototype.get_archive_manifest = function(archiveId, token, callback){
+ this.token = token;
+ this.archiveId = archiveId;
+ var self = this;
+ var parseResponse = function(chunks){
+ var response = new OpenTokSDK.OpenTokArchive(self);
+ var start = chunks.match('<resources>')
+ , end = chunks.match('</resources>')
+ var videoTags = null
+ if(start && end){
+ videoTags = chunks.substr(start.index + 12, (end.index - start.index - 12))
+ attr = videoTags.split('"');
+ if(attr.length>5){
+ vid = attr[1]
+ length = attr[3]
+ name = attr[5]
+ resource = new OpenTokSDK.OpenTokArchiveVideoResource(vid, length, name);
+ response.addVideoResource(resource);
+ }
+ }
+ callback(response);
+ };
+
+ options = {
+ host: this.api_url,
+ path: GET_MANIFEST+archiveId,
+ method: 'GET',
+ headers: {
+ 'x-tb-token-auth':token
+ }
+ }
+ req = https.request(options, function(res){
+ var chunks = '';
+ res.setEncoding('utf8')
+ res.on('data', function(chunk){
+ chunks += chunk
+ })
+ res.on('end', function(){
+ parseResponse(chunks);
+ })
+ })
+ req.end()
+}
+
+OpenTokSDK.prototype.generate_token = function(ops){
ops = ops || {}
// grab options or use defaults:
@@ -88,10 +169,14 @@ OpenTokSDK.prototype.generateToken = function(ops){
tokenParams = ["partner_id=",this.partnerId,"&sdk_version=",SDK_VERSION,"&sig=",sig,":",dataString].join("")
tokenBuffer = new Buffer(tokenParams,"utf8");
return TOKEN_SENTINEL + tokenBuffer.toString('base64');
+}
+
+OpenTokSDK.prototype.generateToken = function(ops){
+ this.generate_token(ops);
}
-OpenTokSDK.prototype.createSession = function(ipPassthru, properties, callback){
+OpenTokSDK.prototype.create_session = function(ipPassthru, properties, callback){
// Since properties is optional, we need to check the number of parameters passed in
if(!callback){
callback = properties
@@ -117,7 +202,10 @@ OpenTokSDK.prototype.createSession = function(ipPassthru, properties, callback){
}
callback(self.sessionId)
});
+}
+OpenTokSDK.prototype.createSession = function(ipPassthru, properties, callback){
+ this.create_session(ipPassthru, properties, callback);
}
OpenTokSDK.prototype._signString = function(string, secret){
Oops, something went wrong.

0 comments on commit 72f18ee

Please sign in to comment.