-
-
Save ToeJamson/7554035 to your computer and use it in GitHub Desktop.
Building Video Calling With PubNub and WebRTC
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <script src=”http://cdn.pubnub.com/pubnub.min.js”></script> | |
| <script src=”http://pubnub.github.io/webrtc/webrtc-beta-pubnub.js”></script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pubnub.subscribe({ | |
| channel: ‘phonebook’, | |
| callback: function (message) { | |
| // Do nothing in our callback | |
| }, | |
| presence: function (data) { | |
| if (data.action === “join” && data.uuid != myUuid) { | |
| var parts = data.uuid.split(‘-’); | |
| var newUser = newUserTemplate({ | |
| name: parts[1] | |
| id: parts[0] | |
| }); | |
| userList.append(newUser); | |
| } else if (data.action === “leave” and data.uuid != myUuid) { | |
| var parts = data.uuid.split(‘-’); | |
| var item = userList.find(“li[data-user=\”” + parts[0] + “\”]”); | |
| item.remove(); | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function gotStream(stream) { | |
| document.querySelector(‘#self-call-video’).src = URL.createObjectURL(stream); | |
| myStream = stream; // Save the stream for later use | |
| }; | |
| navigator.webkitGetUserMedia({ audio: true, video: true}, gotStream); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function publishStream(uuid) { | |
| pubnub.publish({ | |
| user: uuid, | |
| stream: myStream | |
| }); | |
| pubnub.subscribe({ | |
| user: uuid, | |
| stream: function (data, event) { | |
| document.querySelector(‘#call-video’).src = URL.createObjectUrl(event.stream); | |
| }, | |
| disconnect: function (uuid, pc) { | |
| document.querySelector(“#call-video”).src = “”; | |
| $(document).trigger(“call:end”); | |
| } | |
| }); | |
| }; | |
| pubnub.onNewConnection(function (uuid) { | |
| if (myStream != null) { | |
| publishStream(uuid); | |
| } | |
| }); | |
| pubnub.subscribe({ | |
| channel: ‘answer’, | |
| callback: function (data) { | |
| if (data.caller == myUuid) { | |
| publishStream(data.callee); | |
| } | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function hangUp() { | |
| pubnub.closeConnection(currentCall, function () { | |
| $(document).trigger(“call:end”); | |
| } | |
| }; | |
| pubnub.subscribe({ | |
| // ... Other subscribe code here | |
| disconnect: function (uuid, peerConnection) { | |
| hangUp(); | |
| // React to the disconnect | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment