API Scavenger Hunt Recap and Solution

Last weekend we ran a Family Guy Themed API Scavenger Hunt, and this was our question:

Peter Griffin wants to Video Chat with a well known Tech Consultant named Song Zheng to find out what tech trends he should invest in. After scouring the interwebz, a wise cat told him that Song is an evangelist at TokBox and his ApiKey is “25925352”. He is always connected to the session ‘2_MX4yNTkyNTM1Mn4xMjcuMC4wLjF-V2VkIE1heSAyOSAxNzoxMjozNyBQRFQgMjAxM34wLjk0MjQ2NjQ0fg’. To connect to that session, the wise cat has also intelligently acquired the access token:

"T1==cGFydG5lcl9pZD0yNTkyNTM1MiZzZGtfdmVyc2lvbj10YnJ1YnktdGJyYi12MC45MS4yMDExLTAyLTE3JnNpZz00YTNjYjk5ZThhOGVhNTMxOWMyYmY3NzQzNmM5MmRmNTgzNDIyNzc2OnJvbGU9cHVibGlzaGVyJnNlc3Npb25faWQ9Ml9NWDR5TlRreU5UTTFNbjR4TWpjdU1DNHdMakYtVjJWa0lFMWhlU0F5T1NBeE56b3hNam96TnlCUVJGUWdNakF4TTM0d0xqazBNalEyTmpRMGZnJmNyZWF0ZV90aW1lPTEzNjk4NzI3NjAmbm9uY2U9MC42MDQ5NzIyNjY5NDExNDA4JmV4cGlyZV90aW1lPTEzNzI0NjQ3NjUmY29ubmVjdGlvbl9kYXRhPQ=="

To solve this puzzle, you would have to connect to Song’s session and subscribe to his video stream using WebRTC. You might also want to publish your video as well so he can see you.

Congratulations to salazarm, alexc89, themichaellai, shazeline, untrueparadox, 92548, jtbandes, GambleCW, sviswana, mayank94, CoolForCats, alanhorizon, and chrso for solving this challenge!

Unfortunately, the token does not work anymore. However, you might still be interested in the solution. First, you must include our javascript for WebRTC:

[html]
<script src=’http://static.opentok.com/webrtc/v2.0/js/TB.min.js’></script>
[/html]

Next, create a publisher object. Make sure you have a div element with the id “myPublisherDiv” so it can get replaced by your video feed. Replace apiKey with what was given – “25925352”

[javascript]
TB.initPublisher(apiKey, ‘myPublisherDiv’)
[/javascript]

Now we need to create a session object and then connect to it. Think of sessionId as rooms. People connected to the same sessionId, or room id, will be able to publish and subscribe to each others’ streams. First we create a session object, replace the sessionId with what is given – “2_MX4yNTkyNTM1Mn…jk0MjQ2NjQ0fg”

[javascript]
var session = TB.initSession(sessionId);
[/javascript]

After creating a session object, we simply connect to it. To connect to a session, we need a token. Think of token as the key to get into a room. Tokens are always generated for its respective sessionId. Replace apiKey and token with what was given in the question

[javascript]
session.connect(apiKey, token);
[/javascript]

Since session.connect is asynchronous and needs to talk to TokBox’s servers, we need to add an event listener to know when we are connected to a session:

[javascript]
session.addEventListener(‘sessionConnected’, sessionConnectedHandler);
[/javascript]

When we are connected to the session, sessionConnectedHandler function will be called. When we are connected to the session, we should start publishing our video into the session so other people in that session can see us.

[javascript]
function sessionConnectedHandler(e){
session.publish( publisher );
}
[/javascript]

What if there were people already publishing into our session? To see them, all we have to do is to subscribe to their video streams. We will write a for loop and subscribe to all the video streams currently in that session.

[javascript]
function sessionConnectedHandler(e){
session.publish( publisher );
for (var i = 0; i < e.streams.length; i++) {
session.subscribe(e.streams[i]);
}
}
[/javascript]

The complete code can be found here.

These steps are all you need to solve our API Scavenger Hunt Challenge. Hope you find this useful, check out other challenges from API Scavenger hunt!