Communication is an essential part of our daily lives. Chat applications make it easy to interact with people around the world. These chat applications send chats in real-time. This is where WebRTC and WebSockets come into play. They grant us real-time communication, making the sending and receiving of chats done immediately.
In this article, we will learn the difference between WebRTC and WebSockets by building a video chat application.
To follow effectively with the tutorial, be sure you have:
Now that all is set, let's learn about WebRTC and WebSocket.
Web Real-Time Communication (WebRTC) is a technology that enables real-time communication between/among web applications. It allows these web applications to capture video, audio, and other data without the use of a server. This means that WebRTC allows peer-to-peer communication amidst web browsers without the need for an intermediary(server).
WebSocket is a communication protocol that allows real-time communication between web browsers where the server is standing as an intermediary. This means that for communication to occur, a user needs to send the message to the server then the server sends that message to other users (Client-Server communication).
WebRTC and WebSocket are technologies that grant real-time communication but vary in several ways. The major difference between WebRTC and WebSocket is that WebRTC doesn't need a server to carry out its real-time communication while WebSocket needs one. This makes WebRTC faster and preferable for video/audio communication.
Despite WebRTC being faster than WebSocket, it primarily sends data over UDP which causes you to lose some packets of data. Since WebSocket uses TCP, it avails you with data integrity and it's preferable for sending valuable information.
Now that we know the difference between WebRTC and WebSocket, let's take a step further and learn how to use these technologies to build a video chat app.
To build the video chat app, we will use WebRTC and WebSocket. We will use WebSocket to create the signal. Once they are connected, we can now use WebRTC to share the webcams of the connected users.
When the user connects, we would request video and audio permission from the user and display their stream on the page. From here, the user can either enter an existing room or create their room. Once the user creates the room, they would be provided with an ID
which the other user would use to connect to their room.
Once the user creates the room, we send an emit
function to the server, the server creates the room and sends the user a random string, which will be used as the room ID
.
The other user will paste the ID
and click on Join Room
. We will return an error, if the ID
provided doesn't have a room attached or if the room contains another user since we are building a video chat app for 2 users only.
If no error was returned, we will emit an event to the host which will prompt the host to initiate the call. Once the call has been initialized, we will create an offer
and send this offer
to the server. Then the server will signal the other user, passing the offer
as a parameter. The other user receives this offer
, sets it as a Remote Description
, and then emits its offer (answer
) to the server. Once the server receives it, it will emit the answer
to the host, the host will set the answer
as its Remote Description
and voilà
We have a better understanding of how the video chat app works, let's write some codes to see it in action.
Create a folder that will contain all the source code for this application. You can name it as desired, but the folder is named video-chat-app in this tutorial. Open this folder in VS Code and run the following command in the integrated terminal to create a NextJS application.
Once the next app has been created, we can run the command below to spin up the NextJS application.
Now, we will install the dependencies that will be used in this application.
nanoid
- This will be used to create random strings. These random strings will be used as the ID
for each user.
socket.io
and socket.io-client
- This is used to signal the two different users.
Next, run npm run dev
to start the NextJS application. This application can be viewed at localhost:3000.
We have our application up and running, we can now create our video chat interface.
Open the index.js
file in the pages
folder and replace the default code in the index.js
file with the one below.
From the above lines of code, We requested for the user's video and audio stream and then stored it in myVideoRef
. Once the stream is got, we returned the video with a little style.
This tutorial would focus more on the video chat functionality than beautification. So, feel free to design the video chat app to your taste.
Let's add a little styling to the application. Open the globals.css
file in the styles
folder and add the following.
In this section, we would create the socket server and develop the signalling service for the video chat app.
Create a file named socket.js
in the pages/api
directory and add the following lines of code.
This is all we need to create the signalling from our server. Next, we would create the signalling from our front end.
Open your index.js
file in the pages
folder and add the following to listen to all events, emitted from our server.
First, we would initialize the socket for the front end.
The 3 dots
...
in the code block means missing lines of code.
Next, we will create a useEffect
function containing all our socket listening events.
Let's create the handlers for these listening events.
Now let's get started with WebRTC.
To make use of WebRTC, we need to make use of ICE Servers. An interactive Connectivity Establishment (ICE) Server is a protocol used to establish a peer-to-peer connection between two browsers in WebRTC applications. It majorly makes use of Session Traversal Utilities for NAT (STUN) and Traversal Using Relays around Nat (TURN) for media communication.
Click here to know more about ICE Servers
There are many public ICE Servers you can pick from, but for this tutorial, we will make use of the OpenReplay ICE Servers.
To add this ICE Server, open the index.js
file and add the lines of code below.
Once a user gets the room ID
and clicks on the Join Room
button, we will create functionality that will enable the host to start the video call.
Let's create the functionality that will enable users to create and join a room in the index.js
file.
Next, we will create a new function that will handle the creation of a new RTCPeerConnection
, which helps to create peer connections.
After creating the peerConnection
function, we will edit the startCall
function and create a new peer connection using the peerConnection
function.
Above, we created a new peer connection, stored our video stream in the peer connection, created an offer
using the peer connection and emitted the offer to the server.
Once the server receives the offer, it sends the offer
to the other peer. Now, let's receive the offer
and emit the other peer's answer.
As seen above, we got the host's offer
from the server, stored it as the other user's remote description
, created an answer
and sent this answer
to the server.
Immediately after the server receives the answer
, it sends the answer
to the host. Let's receive the answer
and set it as the host's remote description
.
Lastly, we need to add the incoming ice-candidate
to the peer connection.
You can view and test out the application at localhost:3000.
It's time to deploy our application. There are various services out there that we can use to deploy our NextJS application. We can't use Vercel because it doesn't support the use of WebSockets at the time of writing this article. We would have used Heroku but Heroku doesn't have a free tier anymore.
The best option we can use is Render. Render is a unified cloud to build and run all your apps and websites with free TLS certificates, a global CDN, DDoS protection, private networks, and auto deploys from Git.
Visit the official Render website to create a free account. You choose to sign up with Google, GitHub, GitLab, or your email address.
Once you've signed up, click on the New + button close to your avatar and select Web Service to create a new application.
Ensure you have deployed your source code to GitHub
Link your GitHub account and Connect your desired repository.
Next, enter a unique name for the application. In this tutorial, the name of the application is openreplay-video-chat so choose something different.
Make sure that your repository root folder is the NextJS folder. If the NextJS application's folder is in another folder, visit render's docs for directions.
Scroll down to Build Command and edit the build command using npm install && yarn build
.
Once that is done, click on the Free tier, scroll to the bottom and click on Create Web Service to deploy the application.
Once it is done installing all dependencies and building the NextJS application, you will get an output similar to the one below.
We have come to the end of this tutorial. In this tutorial, we learned about WebRTC and WebSockets and use this knowledge to create a video chat application.
The complete code for this project can be found here.