With the advancement of the internet, modern-day web browsers are becoming more powerful every day with the introduction of native APIs like WebGL and WebRTC. In earlier days when we wanted to establish a connection between two web browsers, we needed a server in between transferring requests to and fro. But with WebRTC, Connections can be made from browser to browser without any need for a server.
How does it work?
Let’s go over these one by one,
In order to connect to any peer on the internet, we need to know where the client is located. So when a client wants to stream the data, It will create an offer and put it on the signaling server. This offer contains information about the client. When creating this offer, we must note that not all devices are visible on the public internet and might not have the capability to expose their public IP addresses. It is hard to find the public IP address of the devices that sit behind a NAT (Network Address Translation) layer.
Hence, to avoid the problems included with clients behind NAT we use a protocol called Interactive Connectivity Establishment (ICE). ICE finds a way to get the connection between peers by relying on STUN. A STUN server returns the public IP address of the respective peer which can be further shared over the public internet.
Once we have the public address and are able to establish the peer connection, we no longer need STUN servers. So, we have the public address and are ready to connect. Now a peer will create an offer and ready to connect.
Once peer A is ready to start a communication it creates an offer called SDP (Session Description Protocol) which is basically a JSON object that contains information about the session like the media format and network information
Now that we have an SDP offer from peer A, we want the other peer(s) to accept that offer. The SDP offer will now be sent to the signaling server and the signaling server will take care of sending offers to other peer(s) on the internet who wants to join or connect. Once peer B wants to connect it accepts the SDP offer and sends its IP address for the connection.
Once both parties offer and accept the offer we can transfer the data. Since both peers have Public IP addresses they no longer need any server medium to transfer data between them unless they are behind NAT(Network Address Translation) servers which translates the public IP address to a local IP address keeping the client protected from the outside world. But even with NATs in the picture the communication is done through the P2P mechanism.
Who uses WebRTC?
Many popular streaming services in today’s world are mostly relying on WebRTC for P2P communication. As per Google Chrome’s report, people are streaming more than 214 million minutes on WebRTC per day and the count is only on Google Chrome without including other web browsers.
Some of the tech giants who use WebRTC are as follows,
- Google Meet and Hangouts
- Facebook Messenger
- Amazon Chime
- House Party
WebRTC will be Widely used in Video Conferencing, Online Gaming, etc. Since the server is eliminated, there is almost no network traffic, the cost of servers is reduced and latency is involved in p2p connections.