https://www.tutorialspoint.com/webrtc/webrtc_video_demo.htm
In this chapter, we are going to build a client application that allows two users on separate devices to communicate using WebRTC. Our application will have two pages. One for login and the other for calling another user.
The two pages will be the div tags. Most input is done through simple event handlers.
To create a WebRTC connection clients have to be able to transfer messages without using a WebRTC peer connection. This is where we will use HTML5 WebSockets a bidirectional socket connection between two endpoints a web server and a web browser. Now let's start using the WebSocket library. Create the server.js file and insert the following code
The first line requires the WebSocket library which we have already installed. Then we create a socket server on the port 9090. Next, we listen to the connection event. This code will be executed when a user makes a WebSocket connection to the server. We then listen to any messages sent by the user. Finally, we send a response to the connected user saying “Hello from server”.
In our signaling server, we will use a string-based username for each connection so we know where to send messages. Let's change our connection handler a bit
This way we accept only JSON messages. Next, we need to store all connected users somewhere. We will use a simple Javascript object for it. Change the top of our file
We are going to add a type field for every message coming from the client. For example if a user wants to login, he sends the login type message. Let's define it
If the user sends a message with the login type, we
Check if anyone has already logged in with this username
If so, then tell the user that he hasn't successfully logged in
If no one is using this username, we add username as a key to the connection object.
If a command is not recognized we send an error.
The following code is a helper function for sending messages to a connection. Add it to the server.js file
When the user disconnects we should clean up its connection. We can delete the user when the close event is fired. Add the following code to the connection handler
After successful login the user wants to call another. He should make an offer to another user to achieve it. Add the offer handler
Firstly, we get the connection of the user we are trying to call. If it exists we send him offer details. We also add otherName to the connection object. This is made for the simplicity of finding it later.
Answering to the response has a similar pattern that we used in the offer handler. Our server just passes through all messages as answer to another user. Add the following code after the offer handler
The final part is handling ICE candidate between users. We use the same technique just passing messages between users. The main difference is that candidate messages might happen multiple times per user in any order. Add the candidate handler
To allow our users to disconnect from another user we should implement the hanging up function. It will also tell the server to delete all user references. Add the leave handler
This will also send the other user the leave event so he can disconnect his peer connection accordingly. We should also handle the case when a user drops his connection from the signaling server. Let's modify our close handler
The following is the entire code of our signaling server
One way to test this application is opening two browser tabs and trying to call each other.
First of all, we need to install the bootstrap library. Bootstrap is a frontend framework for developing web applications. You can learn more at http://getbootstrap.com/. Create a folder called, for example, “videochat”. This will be our root application folder. Inside this folder create a file package.json (it is necessary for managing npm dependencies) and add the following
Then run npm install bootstrap. This will install the bootstrap library in the videochat/node_modules folder.
Now we need to create a basic HTML page. Create an index.html file in the root folder with the following code
This page should be familiar to you. We have added the bootstrap css file. We have also defined two pages. Finally, we have created several text fields and buttons for getting information from the user. You should see the two video elements for local and remote video streams. Notice that we have added a link to a client.js file.
Now we need to establish a connection with our signaling server. Create the client.js file in the root folder with the following code
Now run our signaling server via node server. Then, inside the root folder run the static command and open the page inside the browser. You should see the following console output
The next step is implementing a user log in with a unique username. We simply send a username to the server, which then tell us whether it is taken or not. Add the following code to your client.js file
Firstly, we select some references to the elements on the page. The we hide the call page. Then, we add an event listener on the login button. When the user clicks it, we send his username to the server. Finally, we implement the handleLogin callback. If the login was successful, we show the call page and starting to set up a peer connection.
To start a peer connection we need
Obtain a stream from the web camera.
Create the RTCPeerConnection object.
Add the following code to the “UI selectors block”
Modify the handleLogin function
Now if you run the code, the page should allow you to log in and display your local video stream on the page.
Now we are ready to initiate a call. Firstly, we send an offer to another user. Once a user gets the offer, he creates an answer and start trading ICE candidates. Add the following code to the client.js file
We add a click handler to the Call button, which initiates an offer. Then we implement several handlers expected by the onmessage handler. They will be processed asynchronously until both the users have made a connection.
The last step is implementing the hang-up feature. This will stop transmitting data and tell the other user to close the call. Add the following code
When the user clicks on the Hang Up button
It will send a “leave” message to the other user
It will close the RTCPeerConnection and destroy the connection locally
Now run the code. You should be able to log in to the server using two browser tabs. You can then call the tab and hang up the call.
The following is the entire client.js file
This demo provides a baseline of features that every WebRTC application needs. To improve this demo you can add user identification through platforms like Facebook or Google, handle user input for invalid data. Also, the WebRTC connection can fail because of several reasons like not supporting the technology or not being able to traverse firewalls. A worth of work has gone into making any WebRTC application stable.