#mobiletea 3

WebRTC + Node.js

gin + tonic

#mobiletea


  • event hosted by CodeInvaders.net
  • driven with the help of MobileLovers
  • supported by
    • o'reilly
    • adobe
    • blackberry
    • lanificio 159
    • catenate
    • gnstudio
    • ikon
  • made possible by you

about me


  • e-mail: g.natili@gnstudio.com
  • twitter: @giorgionatili
  • blog: webplatform.io (coming soon!)
  • community: codeinvaders.net

agenda


  • What is Real Time Communication
  • WebRTC
    • The Standard
    • Architecture Overview
    • Client Side API
    • Demo
  • Node.js Implementation
    • What is node.js
    • webrtc.io
    • Demo
  • WebRTC and Mobile
  • Conclusions

Real Time Communication


  • RTC is any mode of telecommunications in which all users can exchange information instantly or with negligible latency 

  • RTC can happen in half-duplex or full-duplex mode

  • RTC is sometime used as a synonymous of live 

  • RTC communication includes:
                                      
    • Telephony (land line or mobile)
    • Instant messaging
    • Voice over IP (VoIP)
    • IRC (Internet Relay Chat)
    • Live videoconference

Audio and Video


  • It's usual to send audio/video data in real time applications
  • There are several server on the market able to stream data
     

What is WebRTC



  • Accordingly to google WebRTC is a new way yo do real time communication on the web without using any plugin.

  • WebRTC is a free, open project that enables web browsers with Real-Time Communications (RTC) capabilities via simple Javascript APIs.

  • WebRTC API are available in Chrome 24+, Aurora (FF nightly build) and Opera 12+.

  • Don't confuse WebRTC API and the the <device> API.

What's About IE?


  • Micrsoft will not implement WebRTC, it will go ahead with its standard named CU-RTC-Web

  • CU-RTC-Web stays for Customizable, Ubiquitous Real Time Communication over the Web

  • There is a strong debat about CU-RTC-Web and it seems MS has some good reason to go its own way

    • An RTC standard should provide information about the network quality

    • A standard cannot support only the VP8 video codec

What's About Safari


  • At the moment Apple doesn't have an official position on WebRTC

  • What it’s doing with iMessage and FaceTime suggest it’s going its own way

  • The rumors lead to think that Apple will add WebRTC to Safari at the very least once the specification is finalized

Cross Browser Solution



webrtc4all


Chrome and Firefox


  • Work very well together with WebRTC also if there are some differences in the API

  • The differences will be discussed later after the overview

  • So far keep in mind these browsers work well together 




WebRTC pros 

There are several good reasons to start to use WebRTC especially if compared with today market where most of the components are proprietary.

  • No licences or other fees are needed to start with it

  • The end user doesn't have to download and install additional software

  • Integration is performed using standard API accessed by JavaScript

WebRTC cons


The support is partial and the documentation is very fragmented


Which are the main responsibilities of RTC?


  • Get real-time streaming audio and video or data

  • Communicate real-time streaming audio, video or data

  • Exchange session control messages and media information

What's New in the Standard


  • The APIs are standard and platform independent

  • Using the SRTP protocol it's possible to create encrypted  audio/video communication

  • Communication is more reliable because there is a direct connection between peers using NAT (Network Address Translation)

  • The protocol is adaptive to the network conditions

  • WebRTC can inter operate with communication systems using SIP, Jingle or PSTN 

Key Features


The key features of the WebRTC API are:

  • MediaStreams (aka getUserMedia), access to and control control of the user camera and microphone

  • PeerConnection, negotiate and connect clients in order to allow direct communication

  • DataChannels, peer to peer data exchange

How to Use WebRTC


  1. Obtain a local media from the browser

  2. Setup a connection between a browser and another peer

  3. Attach media and data to channel opened between the peers

Access Camera and Video


  • In order to access the user camera and audio is enough to use the getUserMedia method of the navigator object

  navigator.getUserMedia({video: true, audio:true},
                          onStreamReady, onStreamError);

            
  • The onStreamReady handler receive a MediaStream object

  • The onStreamError handler receive a NavigatorUserMediaError object with a code

Access User Camera Demos


Create a Peer Connection


  • The RTCPeerConnection API represents the core of WebRTC

  • From a practical point of view the "peer" connection is able to connect two points over internet

  • A connection can setup is performed using a configuration object containing the info to peer two clients

Peers Connection Uses ICE


  • Interactivity Connection Establishment is a framework for connecting peers, such as two video chat clients; initially, ICE tries to connect peers directly, with the lowest possible latency, via UDP

  • In this process, STUN servers have a single task: to enable a peer behind a NAT to find out its public address and port


function createPC() {       
pc = new RTCPeerConnection( configuration);         
// send any ice candidates to the other peer       
pc.onicecandidate = function (evt) {               
signalingChannel.send(JSON.stringify({"candidate": evt.candidate }));
};         
// process addition of remote streams       
pc.onaddstream = function (evt) {handleIncomingStream( evt.stream);}; 
}

STUN and ICE


  • STUN (Session Traversal Utilities for NAT) is a standardized set of methods and a network protocol to allow an end host to discover its public IP address if it is located behind a NAT

  • ICE (Interactive Connectivity Establishment) is a framework for connecting peers, such as two video chat clients. Initially, ICE tries to connect peers directly, with the lowest possible latency, via UDP

  • STUN servers have a single task: to enable a peer behind a NAT to find out its public address and port

Peer Connection Demo


WebRTC Uses Servers


  • Users can discover each other and exchange 'real world' details such as names

  • WebRTC client applications (peers) exchange network information

  • WebRTC client applications traverse NAT gateways and firewalls

Signaling


  • Signaling protocols are a way to coordinate and control the communication between peers

  • You can choose whatever messaging protocol prefer, such as SIP or XMPP, to exchange messages

  • Usually the following information are shipped by signaling

    • Session control messages (communication initialization & co.)
    • Network configuration (e.g. computer's IP address and port)
    • Network configuration (e.g. computer's IP address and port)

Signaling Schema



Exchanging Media and Data


  • Once the connection is set up, any number of local media streams may be attached to the Peer Connection for sending across the connection to the remote browser.

  • When a request is made, locally or remotely, to add or remove media, the browser can be asked to generate an appropriate SessionDescription object

  • Once the browsers have exchanged SessionDescription objects, the media or data session can be established (hole punching starts)

Communication Flow



Architecture Overview



What is Node.js


  • A way of running JavaScript outside the browser

  • A wrapper around the V8 Google Chrome JavaScript runtime that provides additional API

    • Buffer
    • FileSystem
    • Many others...

  • A server side event driven environment

Hello Node!


  • Install node http://nodejs.org/
  • Install node packet manager http://npmjs.org/
  • Install http module -> npm install http
  • Create a server.js file

var http = require('http'); 
http.createServer( function (req, res) { 

res.writeHead( 200, {' Content-Type': 'text/ plain'}); 
res.end(' Hello World!\n'); }).listen( 8500, "127.0.0.1"); 

console.log('Server running at http://127.0.0.1:8500/');

Why Node?


  • Easy to start using the built-in Read Evaluate Print Loop tool

  • Because it's more scalable compared to a classical server side environment

  • Because the learning curve is easy for a web developer

  • Because it can be used to run UI app on the desktop too https://github.com/rogerwang/node-webkit (app runtime based on chromium and node.js)

  • Because a node.js app serves up a base presentation document, manipulates it with Javascript, get more data from the server and do it again! (single page apps!)

webrtc.io



 npm install webrtc.io

 It depends on:

webrt.io bootstrap

 
  • var app = require('express')();

  • var server = require('http').createServer(app);

  • var webRTC = require('webrtc.io').listen(server);

  • server.listen(port);



webrt.io handlers


  • webRTC.rtc.on('connect', function(rtc) {
      //Client connected
    });

  • webRTC.rtc.on('chat_msg', function(data, socket) {
    // Chat message handler
    }

Behind the scene there is a node.js WebSocket implementation (http://einaros.github.com/ws/)

    WebSockets vs WebRTC


    • WebRTC defines a media transport over RTP which can work also in Peer to Peer (under certain circumstances), WebSockets don't!

    • WebRTC allows direct communication between browsers, WebSockets don't!

    • In order to establish a WebRTC session you need a signaling protocol defined in order to peer the clients

    • WebSockets are more secure and reliable if the app require a server-client communication

    • WebRTC is designed to share media streams not huge data streams (use WebRTC for a game not for file transfer)



    WebSocket is used for:

     signaling
    and
    messaging







    DEMO

    Mobile WebRTC 


      So far there is no WebRTC support available on mobile using one of the major browsers

      • You have to use an experimental browser from Ericsson named Bowser (more info here)

      • You can play with the online demo http://webrtc.labs.ericsson.net:8082/

      • You can create your own starting from some source code using Node.js and NodeJitsu || Heroku || Nodester


      Thanks! 

      Gracias! 

      謝謝

      Grazie!

      Merci! 

      благодаря! 

      obrigado!

      webrtc

      By Giorgio Natili

      webrtc

      • 5,461