Create a peer to peer chat app with webRTC
Tags: webrtc, javascript, privacy, wen
Published: 2021-12-24 14:55:14

In this article we will use webRTC and socket.io to create a peer to peer chat app between two clients.

**The server**
We need a server for signaling in order to establish a connection between the two peers. The two clients must know where is the other client.

Initialize a new node.js project:
`npm init`

Install express:
`npm install --save express`

Install socket.io for signaling:
`npm install --save socket.io`

Install ejs for server side rendering:
`npm install --save ejs`

Install nodemon for auto rerun after code change
`npm install --save nodemon`

Server code: server.js
This is the simplest server for signaling in order to connect max two peers and one view:
{% gist https://gist.github.com/EneasLari/a73e1972a2861ed35356c8989c727dac file=server_1.js%}

**Code explanation**

- At line 12 we use express and ejs to render a simple html page that will help us to make the UI for the file sharing.

- At line 16 a http server is created and is set to listen to port 3000 or the port defined by a PORT environment variable at line 18

- At line 20 a new instance of socket.io is initialized and we pass http server as a parameter.

- At line 22 we listen at the connection event where we have defined all our other events that occur after connection.

- `function log()` is a function that emits to the client who sent  the message a log event and some message that is defined in the arguments as an array  

Lets talk about sockets events that are defined inside our server code.

- `create or join` when the server receives this event from the client initially at line 44 it gets the number of clients in room.
and stores it in var clientsInRoom, if this variable is null then we define the numClients as zero otherwise we set it as the clientsInRoom. At line 49 if the clients who sends the message is the first then we emit the `created` event to the client otherwise at line 54 if is the second who sends this message we send `join` to the first client, we join the second client and a `joined` event is sent to the second client that just joined the room.
At the end the two clients will receive `ready` event.
If anyone else except these two clients send other `create or join event` he will receive a `full` event.

- `ipaddr` event is for signaling webRTC

**Now for the clients:**

For the client your will need a config.js file which contains a json object for STUN/TURN server. I will not explain in details what is this for in this article but it is necessary to help two clients to exchange their public IP Addresses.
The gist above is not correct for security reasons but you can create your own config object at [xirsys](https://xirsys.com/ )

{% gist https://gist.github.com/EneasLari/a73e1972a2861ed35356c8989c727dac file=config.js %} 

**The client JavaScript:**
{% gist https://gist.github.com/EneasLari/a73e1972a2861ed35356c8989c727dac file=main_1.js %} 

**The view:**
{% gist https://gist.github.com/EneasLari/a73e1972a2861ed35356c8989c727dac file=index.ejs %} 

Edit the package.json to look like this:
{% gist https://gist.github.com/EneasLari/a73e1972a2861ed35356c8989c727dac file=package.json %} 

Run the project

`npm run dev`

Now open two tabs on browser and click connect in every tab , also open debug window to see logging(f12)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8lyxvc9j4lnf5513h3y3.png)
 
Now that we have made a peer to peer connection we can create a data channel so clients can send messages to each other.

We will only modify the client code:
{% gist https://gist.github.com/EneasLari/a73e1972a2861ed35356c8989c727dac file=main_2.js %} 

Lets add a textarea , a send button and printout the messages in the browser

{% gist https://gist.github.com/EneasLari/a73e1972a2861ed35356c8989c727dac file=index_2.ejs %} 

Now run again the project with:

`npm run dev`

After you click connect in two browsers you see that send message buttons are being enabled

Write a message in the textarea, then click send and see the message in the other tab

Congratulation we made a super message app with some incredible UI!

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1qdpdh3delfg8ls66exg.png)
 
[Source Code](https://github.com/tzagiollari/peer_to_peer_chat_app_with_webrtc)

Thanks for your time.
Leave a question or comment below.