WebRTC is a set of protocols, mechanisms and APIs that provide browsers and mobile applications with Real-Time Communications (RTC) capabilities over peer-to-peer connections

WebRTC enables real time communication from browser without any plugin.You do not need install any extra software or plugin in the browser. WebRtc API is defined by W3C.WebRTC enables data,video, auto sharing from browsers over the exiting communication technologies. Data does not need to go through via the central server, In WebRtc two browsers directly communicate with each other.

One of the biggest challenge in peer to peer communication without any mediator is how to locate the ipaddress of peer and create bidirectional communication channel.Peer computers are connected to internet via NAT Routers which acts a gateway.They hide the internal ip address of the computer.Outside world only see the ip address of NAT router and not the ipaddress of the peer computer. If we can traverse the NAT then we can successfully connect two peer browsers and create bi-directional commutation channel and transfer data in real time without any proxy.

WebRtc still needs Server

They way Web as whole is evolved is that we needed web browsers on client side which makes calls to web server and fetch the data over http protocol ,parses the html,css,srcipt and show the content to users. Here web servers runs on the public static address reachable from internet. What if we want to use same tools and protocols to communicate with a peer computers present on some local network. WebRTC specification tries to enable peer to peer communication between two web browsers connected to internet not via static ipaddress but via NAT protocol. The challenge here is how to locate the peer computer which is behind the NAT Server.

Signaling – To exchange metadata to co-ordinate communication
NAT and Firewall.

Signaling

Signaling is process of setting up the connection between browsers. Both browsers exchange metadata between each other so that the connection could be established between them.The goal of signal is the setup Session control, Key-values, Network Ip exchanges etc. Signaling is not defined by the WebRtc, its up the developers whatever protocol they want to use in signaling. Session Description Protocol is used for defining the structure of the message format.These messages are exchanged over Transport protocol e.g Websockets. Web sockets is one of the popular underlying Transport protocol used in signaling.Websockets is full duplex single connection over which messages can be exchanged. Session Description Protocol(SDP)

The SDP Offer/Answer model is a negotiation between two peers of a unicast stream, by which the sender and the receiver share the set of media streams and codecs they wish to use, along with the IP addresses and ports they would like to use to receive the media. WebRtc Signaling Transport

HTTP 1.1
Websockets
WebRtc DataCannels
Http 2

STURN and TURN

Most of the peer to peer clients are behind the NAT.NAT was developed to solve the problem of scare static Ipaddress. But NAT create problem in P2P communication because clients are behind the NAT they can not see the peer’s real ip Address but the router ip addresss.To work peer to peer clients need direct ip address.To solve this issue we need STURN and TURN Servers.

STUN servers live on the public internet and have one simple task: check the IP:port address of an incoming request (from an application running behind a NAT) and send that address back as a response. In other words, the application uses a STUN server to discover its IP:port from a public perspective. This process enables a WebRTC peer to get a publicly accessible address for itself, and then pass that on to another peer via a signaling mechanism, in order to set up a direct link

Client communicate with STURN servers to get the peers real ip address in order to traverse NAT and firewall.Some times STURN fails because of the NAT configurations, firewalls etc then we TURN server are used which acts as relay for the data transfer.Signaling, STURN and TURN are sometimes packed into one server and its called ICE Server(nteractive Connectivity Establishment). This ICE server takes care of the signaling and NAT Traversal and fallback mechanism and give streamline connectivity with peers.

Turn Servers as has one simple task to Relay the stream. They use lot of bandwidth. They acts as proxy for packets between the peers. WebRTC WebAPI

W3C defines a set of APIs for web developers who wants add support of webrtc in user facing applications and also for Web browser vendors.

Important APIs getUserMedia RTCPeerConnection RTCDataChannel getUserMedia()

Access media devices. This API is used for taking permission from user to access Video Camera and Audio devices. It returns a Promise that resolves to a MediaStream object. If the user denies permission, or matching media is not available, then the promise is rejected with NotAllowedError or NotFoundError respectively.


<script>
const constraints = {
  video: true
};

navigator.mediaDevices.getUserMedia(constraints)
.then(function(stream) {
  /* use the stream */
})
.catch(function(err) {
  /* handle the error */
});
</script>

RTCPeer​Connection

The RTCPeerConnection interface represents a WebRTC connection between the local computer and a remote peer. It provides methods to connect to a remote peer, maintain and monitor the connection, and close the connection once it’s no longer needed. Check example below.


var restartConfig = { iceServers: [{
                          urls: "turn:asia.myturnserver.net",
                          username: "allie@example.com",
                          credential: "example"
                      }]
};

myPeerConnection.setConfiguration(restartConfig);

myPeerConnection.createOffer({"iceRestart": true}).then(function(offer) {
  return myPeerConnection.setLocalDescription(offer);
})
.then(function() {
  // send the offer to the other peer using the signaling server
})
.catch(reportError);

RTCDataChannel API

The RTCDataChannel interface represents a network channel which can be used for bidirectional peer-to-peer transfers of arbitrary data. Every data channel is associated with an RTCPeerConnection.

Underlying data format in RTCDataChannel can be “UDP/DTLS/SCTP” or TCP/DTLS/SCTP.

var pc = new RTCPeerConnection();
var dc = pc.createDataChannel("my channel");

dc.onmessage = function (event) {
  console.log("received: " + event.data);
};

dc.onopen = function () {
  console.log("datachannel open");
};

dc.onclose = function () {
  console.log("datachannel close");
};