Skip to content

Commit da82f52

Browse files
committed
update helloworld sample to use more modern JS APIs, fixes #83
1 parent dbfd4df commit da82f52

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed
Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,29 @@
11
// Initialize an OpenTok Session object
2-
var session = TB.initSession(sessionId);
2+
var session = OT.initSession(sessionId);
33

44
// Initialize a Publisher, and place it into the element with id="publisher"
5-
var publisher = TB.initPublisher(apiKey, 'publisher');
5+
var publisher = OT.initPublisher(apiKey, 'publisher');
66

7-
// Attach event handlers
8-
session.on({
7+
// Attach an event handler for when the session dispatches the 'streamCreated' event.
8+
session.on('streamCreated', function(event) {
9+
// This function runs when another client publishes a stream (eg. session.publish())
910

10-
// This function runs when session.connect() asynchronously completes
11-
sessionConnected: function(event) {
12-
// Publish the publisher we initialzed earlier (this will trigger 'streamCreated' on other
13-
// clients)
14-
session.publish(publisher);
15-
},
11+
// Subscribe to the stream that caused this event, put it inside the DOM element with id='subscribers'
12+
session.subscribe(event.stream, 'subscribers', {
13+
insertMode: 'append'
14+
});
15+
});
1616

17-
// This function runs when another client publishes a stream (eg. session.publish())
18-
streamCreated: function(event) {
19-
// Create a container for a new Subscriber, assign it an id using the streamId, put it inside
20-
// the element with id="subscribers"
21-
var subContainer = document.createElement('div');
22-
subContainer.id = 'stream-' + event.stream.streamId;
23-
document.getElementById('subscribers').appendChild(subContainer);
17+
// Connect to the Session using the 'apiKey' of the application and a 'token' for permission
18+
session.connect(apiKey, token, function(error) {
19+
// This function runs when session.connect() asynchronously completes
2420

25-
// Subscribe to the stream that caused this event, put it inside the container we just made
26-
session.subscribe(event.stream, subContainer);
27-
}
21+
// Handle connection errors
22+
if (error) {
23+
return console.error(error);
24+
}
2825

26+
// Publish the publisher we initialzed earlier (this will trigger 'streamCreated' on other
27+
// clients)
28+
session.publish(publisher);
2929
});
30-
31-
// Connect to the Session using the 'apiKey' of the application and a 'token' for permission
32-
session.connect(apiKey, token);

0 commit comments

Comments
 (0)