TokBox tout de suite
Trying to play around with TokBox (TB) during a hackathon, but I'm missing a more complete tutorial… I mean, their Hello World's nice, but a bit sparce for our time frame, and excitement? (TB's fantastic Evangelist, Ankur, was there to help us, but everyone wanted a bite! ;o)
Anyway, here's an attempt at a quick and dirty tutorial. And a working demo?
Ideas?
- Lets do a group video-chat thingy. Like, one click to start a video chat embedded into a Web page.
- Peer-to-peer? TokBox "wraps" WebRTC, among other things, so I immediately thought sure, P2P would be so cool, pure client-side, especially for my static HTML demo, but… Realistically, we'd need something on the server side, won't we? (Though tokens can be generated client-side, too.)
- CoffeeScript! ;o) Well, server-side, obviously, but maybe also in the browser? Later.
- Meteor?! Just to serve some static HTML for this toy demo? Because can't use the camera, or WebRTC, or something, with file:///… Wait, just "wrap" our index.html with
$ cd $MOCKUP_DIR $ python -m SimpleHTTPServer
and browse to http://localhost:8000/. Done. ;o)
Process
- Setup dance routine: register for an API key, "session ID". And a session and temporary token just for the hackathon?
- Inject TB's API into the HTML:
<script src="http://static.opentok.com/webrtc/v2.0/js/TB.min.js" ></script>
- Session ID and token: my demo is pure client-side, so I've used the dashboard to generate a session ID and a temporary token.
- Connect:
var session = TB.initSession("2_MX4z...R-"); session.connect("3144...", "T1==cGFy...T0=");
- Controls and DOM for TB's widget(s?):
<img class="publish-stream" src="lib/camera_black.png"> <p id="see-myself"></p> <p class="streams-container"></p>
And enable these controls only after a session/connection is made:function sessionConnectedHandler(event) { // Enable controls. $(".publish-stream").click(function() { session.publish("see-myself", {width:90, height:50}); }); // See others' videos. subscribeToStreams(event.streams); }
- Publish, ie start broadcasting my webcan:
$(".publish-stream").click(function(){ session.publish(); });
- Subscribe, ie show peers when they start broadcasting: …
- Debugging:
TB.addEventListener("exception", function(event) {alert(event.message);});
Resources
- TokBox's OpenTok API
- WebRTC tutorial
Feedback
…
--
The real world is a special case