Comprehensive guide to WebRTC. Part 3/5.

In this tutorial we are going to create a basic signalling server using javascript and nodejs. It will be able to connect two users together.

Assume we have 2 users UserA and UserB the scheme of our server will be:
1. Each user will start by registering themselves on the server(simple string login).
2. Now UserA can call UserB. UserA makes an offer with the user identifier(UserB) he wishes to call.
3. The other user(User B) should answer.
4. In the end ice candidates are sent between users so they can make a connection.
Remember that there are no rules implementing a signalling server.

First of all you should install node.js on your computer. To test it create an index.js file and put the following code:

Then run it using node index.js. You might see the output phrase in the console.

We are going to use WebSockets – a bidirectional socket connection between a web browser and a web server. Run npm install ws to install WebSockets library for node.js.

Let’s start using it. Insert the following code in an index.js file:

To test our server install wscat. Run npm install -g wscat. This will install wscat globally so you will be able to access it through a command line.

Run our server in one console window. Then open a new console window and run wscat -c ws://localhost:9090. Then try to send a message to the server.

Let’s add a simple string based auth. Modify index.js:

Now connect to our server and try to login:
run wscat -c ws://localhost:9090
run {"type":"login","name":"UserA"} when connected

Let’s implement the offer handler, which is called when one user want to call another:

The answer handler is called when user answers to someone’s offer:

The final part is handling ICE candidates between users. Remember that candidate messages might happen multiple times between users. Add candidate handler:

A good feature is implementing the leave handler which will allow our users to disconnect from another user and notify our server to disconnect any user references. Add the leave handler:

To handle a case when a user drops a connection we need to modify the close handler:

The entire code of our signalling server:

In the real world, signalling is not defined by the WebRTC specification. So there are a few glitches like complex firewall systems or VPN(Virtual Private Network) when you fall back on other technologies such as HTTP instead of WebSockets. I also advise you to check XMPP and SIP protocols which give a lot of power to any typical WebRTC app and SIP-based phone devices integration.

That’s all for today 🙂

1 Comments

  1. Pingback: Comprehensive guide to WebRTC. Part 5/5. - ryzhak.com

  2. Передаем Вам сведения о том что принято решение выплатить Вам денежный бонус. Настоятельно рекомендуем прямо сейчас в срочном порядке про

    Писменно сообщаем Вам о том что принято решение выплатить Вам денежные средства. Рекомендуем прямо сейчас не откладывая пройти шаги перейдя по ссылке на основную страницу нашего сервиса в срок до 2 дней. Не пропустите срок!Переход на страницу: https://google.com

Leave a Reply

Your email address will not be published. Required fields are marked *