Tech Blog

Technology

An Introduction to WebRTC

By Anantheshwar4 min read
← Back to blog

Overview

WebRTC is technology which can help us achieve real time communication between two web browsers or an application that works on top of open standards. We can use this technology to build messaging applications, voice and video calling applications as it supports voice, video and generic data transfer.

WebRTC is supported by major browsers like Chrome, Firefox, Opera, Safari and Edge. WebRTC is also supported by mobile device platforms such as Android and iOS using libraries having the same functionality. On top of all this, it is open source and is backed by big tech companies like Google, Microsoft, Apple and Mozilla.

Why use webRTC in our application? 

WebRTC helps you create peer to peer connections between two different clients without a need for third party servers. Hence communication your application will be completely decentralized without requirement of any servers to handle the communication. 

How does it work?

 The first peer will create an offer for another peer to connect to them in terms of Session Description Protocol(SDP) with all necessary data for connection to happen.This will be stored in the server which can be read by another peer to take the request and answer with SDP. Hence the server will act as mediator between two peers with connection but will not interfere with the actual data getting communicated. But the problem with peer to peer communication is that most devices have firewalls. Also the ip addresses change due to Network address translators(NAT). Hopefully we have something called Interactive connectivity establishment(ICE), which helps clients with discovery of their public ips’. Both peers will create the list of their ICE candidates with ips’ and ports with which it can create a connection. In the backround WebRTC will do this by making a series of requests to stun servers. These ICE candidates will be stored in  the database in the server and the algorithm will determine which will be best for creating the connection.

How to get started?

Lets see how we can make connections between two browsers(say peer A and peer B), using WebRTC with javascript. Actually it is quite simple since the browser has built in support for webRTC. Initially peer A creates a new peer connection with help of the server. Then the next step is to create an offer to connect and will set it as a local description. Also we will be creating a data channel to be used for communication. 

We can create peer connection using below function which will take stun server details

const pc = new RTCPeerConnection(servers);

We can create a channel through the following function which will be connected to a peer and will be used to send and receive data. 

const dc = new pc.createDataChannel (“channel”);

Next we can create offer and store it as local description

pc.createOffer.then(x => pc.setLocalDescription(x));

Similarly we have to create a connection in peer B which answers to the offer. As we did for peer A we will create a new connection with the same server and will set the offer received as a remote description. Next we will create an answer and set it as a local description in peer B. 

Following function can be used to set remote description the offer received by peer A and peer B

const rc = new RTCPeerConnection(servers);

rc.setRemoteDescription(offer);

To create the answer to the offer by peer A, Peer B can use following method and store as its local description

rc.createAnswer().then( y => y.setLocalDescription(y));

Now the final step of making the connection is setting the answer we got from peer B as a remote description in peer A.

pc.setRemoteDescription(answer);

With this connection would be established successfully between two peers and you can send messages using the channel created. 

Should you be using webRTC then?

Yes, with fast communication and easy to build upon standardized api which even comes out of the box with modern browsers. However the peer to peer communication gets too complicated when it comes to multiple participants.

Keep reading

You may be interested in

Browse all articles →
Using SCEP Certificates with Okta and SureMDM for Zero Trust Security
August 5, 20257 min read

Using SCEP Certificates with Okta and SureMDM for Zero Trust Security

For businesses embracing zero-trust principles, secure certificate-based authentication isn’t just nice to have—it’s a necessity. With Okta as your Identity Provider (IdP) and SureMDM as your unified endpoint management solution, you can now seamlessly distribute and manage device certificates using Simple Certificate Enrollment Protocol (SCEP). This blog will guide you through: Why Use SCEP with […]

By Sunny KumarRead More →
How to Use mc CLI for Bucket Management and Credential Setup
June 12, 20252 min read

How to Use mc CLI for Bucket Management and Credential Setup

MinIO client The MinIO Client, commonly known as mc, is a command-line tool used to interact with MinIO, a high-performance object storage system. The mc client allows users to manage buckets and objects on the MinIO storage service.

By Malkeet SinghRead More →
How to Setup Passwordless SSH Login for ADE
March 18, 20253 min read

How to Setup Passwordless SSH Login for ADE

To set up a passwordless SSH login in Linux all you need to do is to generate a public authentication key and append it to the remote hosts ~/.ssh/authorized_keys file. The following steps will describe the process for configuring passwordless SSH login: 1. Check for existing SSH key pair Before generating a new SSH key […]

By Mohan Kumar KMRead More →