What is WebRTC? (Peer To Peer in Web)

Janith Kasun
IDEA CORE
Published in
5 min readSep 20, 2018

In this article, we will get to know what is WebRTC, how it works, and where to use it, the basics.

I have a 2 GB video file that I want to share with my friend. So what I would do is open Google drive (or Dropbox or any other cloud storage service), then upload my file to the drive. Then get a shared link from the drive and share it with my friend.

So behind the scene, what happens is your computer will create a TCP connection with a Google server, then upload your file. So your computer will upload 2GB of content, and Google drive will download 2GB of content. After that, when someone needs to download your file, he/she will create a TCP connection with a Google server and request the file. So the Google server will upload 2GB of content and the receiver will download 2GB of content. Simple right!!

This is the extract same, for a messenger call or a Skype video chat. The video/audio will stream to the server and then to your receiver.

This is called the server, client architecture. But there are few drawbacks,

  1. You need to use a centralized service, which may charge you for the service.
  2. The round trip is long (your information needs to be uploaded from you to the server which could be in New York and then to your friend who could be sitting next to you in Antarctica), so the communication delay will be higher compared to a direct connection with your receiver.
  3. There is a data wastage since the server acts like a proxy
  4. Privacy issues, that you have to expose your file to a third-party service provider(the Google drive in this scenario)

But what if there is a way, that you can send your file directly to your friend. No third parties, no need to download any software but using the same exact web browser.

I’m not saying that P2P is the most perfect solution for that situation as well, there could be drawbacks too. One of the disadvantages is that you need to be present online at the moment when the receiver downloading the file.

Personally, I love P2P technologies including torrent. Comparing to HTTP and Web Sockets, WebRTC is less popular among developers. When I first heard that there is a peer to peer communication protocol in my browser, I was amazed.

WebRTC, (The Web Real-Time Communication Protocol) is the web version of peer to peer communication. It is an opensource project which is developed at Google. Currently, there are thousands of passionate developers around the world who innovate new things using WebRTC and it is widely used for video conferencing and audio calls.

So Doesn’t WebRTC need any servers?

Even though the WebRTC protocol is a peer to peer communication protocol, it needs a few types of servers like STUN and TURN to support the protocol.

When you need to connect any computer that is connected to the internet, you need to connect them via public IP and the given port, at least the domain name. This is okay with servers, you know their IP or domain name (ex.. facebook.com) already and their firewalls are configured with access. They have assigned a public IP and a domain name in order to make it easier for you to access them, just type google.com on your browser, you are connected.

But this is not the same with a computer that is connected to a public WiFi connection, shared internet connection or a home internet connection. In this case, the public IP of the gateway(router) is shared between all connected computers and you need to go and configure your router with NAT (Network address translation) which is a stressful task. Then you need to know your receiver's IP address and finally, you can connect.

But this is not possible, if we want to start a video chat with someone, right? Why would anyone want to do this, it is not the 90’s anymore.

Don’t worry!! with WebRTC, the receiver does not need to go through this configuration. Because these support servers are addressing that issue.

STUN Server (The signaling server)

The STUN (Session Traversal Utilities for NAT) is used to discover your public session information and whether or not your peer can connect you directly behind your firewall or router. Then your client is fed with your session information so the direct connection can be established.

Diagram STUN

TURN Server

Major amount of the firewalls allow connection with computers behind the firewall, The success rate is about 80%. However what if your firewall does not allow direct connections.

TURN (Traversal Using Relays around NAT) servers are used as a fallback in this scenario. So it will act as a proxy server for data communication between peers. But in this case, as your content goes through the server you may not have some advantages that I mentioned before.

But this is an optional server, maybe in your scenario failure could be an option. Let’s assume that you are developing the torrent on the web (which actually exists). So failure is an option. Every peer in the network may not need to seed the file as there are other peers who have the same file. So failure is an option.

But think you are developing a video chat service, in that case, you must send your video stream to your client. So failure is not an option, which means you have to host a TURN server.

Diagram TURN

So that’s it for now. Let’s talk about how to implement this in code in my next article.

Thank you.

References:

https://developer.mozilla.org/en-S/docs/Web/API/WebRTC_API/Protocols

--

--

Janith Kasun
IDEA CORE

Developer and Gamer who wants to make an impact