Top Banner
WEBRTC + SOCKET.IO BUILDING A SKYPE-LIKE VIDEO CHAT WITH NATIVE JAVASCRIPT / MICHELE DI SALVATORE @MIKDISAL Javascript Architect @ Objectway
47

WebRTC + Socket.io: building a skype-like video chat with native javascript

Jan 07, 2017

Download

Software

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: WebRTC + Socket.io: building a skype-like video chat with native javascript

WEBRTC + SOCKET.IOBUILDING A SKYPE-LIKE VIDEO CHAT WITH

NATIVE JAVASCRIPT

/ MICHELE DI SALVATORE @MIKDISALJavascript Architect @ Objectway

Page 2: WebRTC + Socket.io: building a skype-like video chat with native javascript

Audio and video communication and peer-to-peer data

sharing through a web application

Native javascript (no plugins)

Open source

appear.in

Page 3: WebRTC + Socket.io: building a skype-like video chat with native javascript

HOW IS BORNGlobal IP Solutions acquired by Google in 2010

Google open sourced the technologies

Page 4: WebRTC + Socket.io: building a skype-like video chat with native javascript

SUPPORTED BROWSERS AND PLATFORMS

Page 5: WebRTC + Socket.io: building a skype-like video chat with native javascript

IE AND SAFARI? freeTemasys Plugin

Page 6: WebRTC + Socket.io: building a skype-like video chat with native javascript

JAVASCRIPT WEBRTC APIMediaStream

RTCPeerConnection

RTCDataChannel

Page 7: WebRTC + Socket.io: building a skype-like video chat with native javascript

MEDIASTREAM (AKA GETUSERMEDIA)Acquiring audio and video

Can contain multiple 'tracks'var constraints = { video: { mandatory: { minWidth: 640, minHeight: 360 } };

function successCallback(stream) { var video = document.querySelector("video"); video.src = window.URL.createObjectURL(stream); }

function errorCallback(error) { console.log("navigator.getUserMedia error: ", error); }

Constraints controls the contents of the MediaStream

Page 8: WebRTC + Socket.io: building a skype-like video chat with native javascript

RTCPEERCONNECTION

Communicating audio and video

Signal processingCodec handlingPeer to peer communicationSecurityBandwidth management...

Page 9: WebRTC + Socket.io: building a skype-like video chat with native javascript

RTCDATACHANNEL

Communicating arbitrary data

Same API as WebSocketsUltra-low latencyUnreliable or reliableSecure

Page 10: WebRTC + Socket.io: building a skype-like video chat with native javascript

SIGNALINGExchanging 'session description' objects

What formats I support, what I want to sendNetwork information for peer-to-peer setup

Any message mechanism and protocol

Page 11: WebRTC + Socket.io: building a skype-like video chat with native javascript

JSEPJavaScript Session Establishment Protocol

Page 12: WebRTC + Socket.io: building a skype-like video chat with native javascript

RTCSESSIONDESCRIPTION EXAMPLE

v=0 o=- 7614219274584779017 2 IN IP4 127.0.0.1 s=- t=0 0 a=group:BUNDLE audio video a=msid-semantic: WMS m=audio 1 RTP/SAVPF 111 103 104 0 8 107 106 105 13 126 c=IN IP4 0.0.0.0 a=rtcp:1 IN IP4 0.0.0.0 a=ice-ufrag:W2TGCZw2NZHuwlnf a=ice-pwd:xdQEccP40E+P0L5qTyzDgfmW a=extmap:1 urn:ietf:params:rtp-hdrext:ssrc-audio-level a=mid:audio a=rtcp-mux a=crypto:1 AES_CM_128_HMAC_SHA1_80 inline:9c1AHz27dZ9xPI91YNfSlI67/EMkjHHIHORiClQea=rtpmap:111 opus/48000/2 ...

Page 13: WebRTC + Socket.io: building a skype-like video chat with native javascript

P2P IN REAL WORLD

NAT & Firewalls?

Page 14: WebRTC + Socket.io: building a skype-like video chat with native javascript

STUN SERVERGives my public IP addressSimple server, cheap to runData flows peer-to-peer

Page 15: WebRTC + Socket.io: building a skype-like video chat with native javascript

STUN SERVER

Page 16: WebRTC + Socket.io: building a skype-like video chat with native javascript

TURN SERVERFallback for p2p failingData via serverCall works everywhere

Page 17: WebRTC + Socket.io: building a skype-like video chat with native javascript

STUN + TURN SERVERS

Page 18: WebRTC + Socket.io: building a skype-like video chat with native javascript

ICEInteractive Connectivity Establishment

A framework to connect peers via UDP, enablingRTCPeerConnection via STUN and TURN server

Page 19: WebRTC + Socket.io: building a skype-like video chat with native javascript

WEBRTC SECURITYOn Chrome only via https

Encryption for media and data

Sandboxed

Page 20: WebRTC + Socket.io: building a skype-like video chat with native javascript

WEBRTC LIBRARIES & CO.

by by by

by ...

adapter.js webrtcrtc-everywhere contraSimpleWebRTC &YetRTCMultiConnection Muaz Khan

Page 21: WebRTC + Socket.io: building a skype-like video chat with native javascript

RTCMULTICONNECTION

A javascript wrapper library supportingapproximately all possible peer-to-peer

features.

WEBRTC EXPERIMENTS

Tons of open source experiments based onRTCMultiConnection:

webrtc-experiment.com

Page 22: WebRTC + Socket.io: building a skype-like video chat with native javascript

WEBSOCKETS

Standardized in 2011

Interactive communication session between browser andserver

Event driven

Sending strings and binary data

Page 23: WebRTC + Socket.io: building a skype-like video chat with native javascript

WEBSOCKETS BROWSER SUPPORT

All and IE10+

ajax polling as fallback

Page 24: WebRTC + Socket.io: building a skype-like video chat with native javascript

SOCKET.IO

A popular open source library with client and serverimplementations

Server side written in Node and easy to startvar server = require('http').createServer(); var io = require('socket.io')(server);

io.on('connection', function(socket){ socket.on('hi!', function(data){ socket.emit('a-message', {hello: 'world'}); });

socket.on('disconnect', function(){}); });

server.listen(3000);

Page 25: WebRTC + Socket.io: building a skype-like video chat with native javascript

SOCKET.IO CLIENT

var socket = io.connect('http://localhost:3000/');

socket.on('connect', function () { socket.emit('hi!'); });

socket.on('a-message', function (data) { console.log(data.hello); });

Page 26: WebRTC + Socket.io: building a skype-like video chat with native javascript

GOALA SKYPE LIKE CHAT

Page 27: WebRTC + Socket.io: building a skype-like video chat with native javascript

MAIN ISSUESWebRTC architecture like IRC

rooms with shared chat: text, video and datadirect chat: only one to oneroom managed by initiatorconversation type can change without agreement: text tovideo

Page 28: WebRTC + Socket.io: building a skype-like video chat with native javascript

IMPLEMENTATION

Page 29: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 1

UserA is logged in and is connected to the socket server

Page 30: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 2

The app sends a "presence" event to other users throughthe socket

The server forwards the message only to user's contacts

Each present user replies with the same "presence" event

Page 31: WebRTC + Socket.io: building a skype-like video chat with native javascript

SOCKET.IO FORWARD ROLE

Page 32: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 3

Each present user replies with the same "presence" event

Page 33: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 4

When a user receives a "presence" event, it automaticallyknows that the sender is online

Also the server sends the “presence” event to all, when auser socket is disconnected

Page 34: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 5

UserA starts a conversation and the app creates the webrtcconnection

Page 35: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 6

UserA sends a message via webrtc

Page 36: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 7

UserB receives a notification

Page 37: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 8

UserB starts writing a message

Page 38: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 9

UserA receives the "is typing" info sent via webrtc

Page 39: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 10

UserA starts a video call sending a request to UserB viasocket

Page 40: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 11

UserB receives the request and accepting starts a video callvia webrtc

Page 41: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 12

The 2 users can video chat via webrtc

Each user can close the video at any time or start a text chat

Page 42: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 13

A user can send text messages also

Page 43: WebRTC + Socket.io: building a skype-like video chat with native javascript

STEP 14

A user can start an audio chat in the same way as a videochat

Page 44: WebRTC + Socket.io: building a skype-like video chat with native javascript

chat

BEHIND THE SCENES

Page 45: WebRTC + Socket.io: building a skype-like video chat with native javascript

WHERE IS WEBRTC?

Behind this actions

chat messagesvideo / audio call"is typing" info

Page 46: WebRTC + Socket.io: building a skype-like video chat with native javascript

AND THE OTHERS?via WebSockets

presencevideo / audio initializationall status infos

Page 47: WebRTC + Socket.io: building a skype-like video chat with native javascript

THAT'S ALL!THANK YOU ALL

/ Michele Di Salvatore @MikDiSal