WebRTC Basics and Project Introduction
Tags: webdev, networking, programming, javascript
Published: 2023-07-20 20:58:38

I am sure all of us have wondered some what or another how is data being transferred over the internet.
We went from transferring files over bluetooth to now video calling our friends in the other side of the world with a seamless transition.

If you are a bit experienced in network theory and how it works, you might have a brief or very good understanding of what protocols are such as TCP/IP, UDP and what are connection requests, DNS, public IP's, private IP's etc.

I am also very inexperienced in this , so please do not take this article as the sole right information and please do your own research along side as well. I am merely writing this so that I can increase my own understanding by typing it out. This article is inspired by [this article](https://medium.com/agora-io/how-does-webrtc-work-996748603141). I suggest reading it out as well as it's a great article.

---

**_Now that we have gotten over what we are doing part, Let's start with the prerequisites._**

### 1. What is WebRTC?
`WebRTC (Web Real-Time Communications) is an open source project that enables real-time voice, text and video communications capabilities between web browsers and devices. WebRTC provides software developers with application programming interfaces (APIs) written in JavaScript.`

_Let's put it in simpler words._

Whenever we want to send a stream of data that is in the form of video/audio or real time voice communication we use WebRTC.
It is a javascript API(Application Programming Interface) that let's us do a real time  peer to peer communication between two devices.

**_What's a peer to peer communication?_**

`A peer-to-peer (P2P) service is a decentralized platform whereby two individuals interact directly with each other, without intermediation by a third party. `

### 2. What are web Sockets?
Since we know what is webRTC, why do we need to know web Sockets.
We need to understand the basic of why we are using webRTC in real time communication instead of web Sockets.
Unline webRTC, webSockets use the help of third party servers to connect two devices.
**_Major Differences:_**
- WebSockets use a third party server to establish and maintain connection.
- WebSockets use TCP protocols for transferring data while webRTC uses UDP protocol.
- TCP protocol validates the data that is being transferred whereas UDP does not. Here we compromise latency for data loss.
- Since we are transferring video/audio a little bit of data loss is something we can work with. If we have to send important files we will use web Sockets for that.
- webRTC also has no built in signalling. We will need to use web Sockets to establish and signal a connection.
- ![WebRTC vs WebSocket](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qafwmnk814e34n4lgo2w.png)


### 3. What is a SDP?
So what exactly are we sending between two devices and how are we sending it.
This is an important question that we should answer before.
We first want to establish a connection between two devices so that they know about each other/ they can identify each other.
Say You are texting someone , and you might have the wrong no.
You first send a message to identify if it's the right connection.
So first we must establish a connection b/w two devices using signaling.

There are two maintain components to it:
- **_A SDP:_** `An SDP is Session Description Protocol- We must first exchange an SDP which contains information about us and our data types such as codec file types.`
- **_An ICE candidate:_** `It is a public IP address that could potentially be an address that receives data.`

### 4. How we are using ICE candidates?

When two devices want to communicate with each other over the internet, they need to follow a few steps to set up the connection properly. First, they exchange some information called "SDP" using a method known as signaling. This allows them to understand each other's capabilities and preferences for the communication.

After exchanging the SDP, the devices are technically connected to each other, but they still can't send data directly. The reason for this is that many modern devices are protected by firewalls and NAT devices, which act as barriers to direct communication.

To overcome this issue and enable data exchange, we use a method called ICE, which stands for Interactive Connectivity Establishment. ICE helps the devices discover each other's public IP addresses and establish a suitable path for data transmission.

So in the background once SDP offers are exchanged each peer will then make a series of requests to a STUN server which will generate a list of ICE candidates to use.

So our user 1 sends a request to the STUN server to get its public ip address and sends it over to the user 2.
Our user 2 accepts that ICE candidate and also requests its stun server to generate ice candidate for it and sends it back.
All of this is happening simultaneously to avoid delay.

I suggest reading about what STUN servers are here: [**_STUN Servers_**](https://www.3cx.com/pbx/what-is-a-stun-server/)


### Overview:
This is it for this article. We covered some basics about what is webRTC, Why we use it over web sockets for media transmission and what are ICE candidates and how you use them.
## Content Referrals:

- [WebRTC](https://www.techtarget.com/searchunifiedcommunications/definition/WebRTC-Web-Real-Time-Communications)
- [P2P](https://www.investopedia.com/terms/p/peertopeer-p2p-service.asp)
- [SDP](https://www.3cx.com/pbx/sdp/#:~:text=SDP%20is%20the%20abbreviation%20for,the%20IETF%20as%20RFC%204566.)
- [ICE Candidate](https://developer.mozilla.org/en-US/docs/Web/API/RTCIceCandidate)

---


If you liked this article , Please drop a comment below as it would mean a lot to me. Feedback is always and highly appreciated.