I already explained the importance of a WebRTC signaling server through my last post . Consequently lets move to the programming section of webRTC . In this post i want to explain a simple basic signaling server with Node.Js web socket method . If your new to the term Node.Js and webSocket , please just go through the basics of Node.Js and webSocket .
If your new to this webRTC series , please check the previous especially relevant posts
WebRTC introduction : Introduction to the browser technology
furthermore check other post too
getusermedia API : browser getusermedia API
RTCPeerconnection API : RTCPeerconnection API
What is signaling server ?
Signaling server helps to identify and collect sharing information of the peer user . Meanwhile check the our previous guide about signaling server and protocols and signaling process in webRTC .
What is web server ?
I think most of all familiar with the word web server . isn’t ? If not , web server is a program that serve different pages to users via HTTP (Hypertext Transfer Protocol ) request . In WebRTC , We uses a signaling server to get the information of the peer users .
Why WebSocket ?
WebSocket is one of the asynchronous method to communicate between the server and the client . The communication takes place over TCP socket using ws (unsecure) or wss (secure) protocol . WebSocket is currently implemented in all latest browsers .
Why Node.Js ?
Node.Js is an open source server framework . It can run on different Operating system (Windows , Linux , MAC etc . ). It uses javascript on the server to run the framework . The benefit of Node.Js includes
- Asynchronous Input output communication .
- It is a Event driver I/O server-side javascript environment .
- It is a very fast due to the javascript in the back end .
Afterwards you can check more about node.Js
Start a basic webRTC signaling server
Next let’s move to the programming of a signaling server with Node.Js webSocket . This is the basic code using webSocket with Node.Js .
var WebSocketServer = require('ws').Server, wss = new WebSocketServer({ port: 8888 }); wss.on('listening', function () { console.log("Server started..."); }); wss.on('connection', function (connection) { console.log("User connected"); //message function connection.on('message', function (message) { console.log("message from user"); }); //close the connection connection.on('close', function () { console.log("Disconnecting user"); }); });
This code snippet handle open, message and close connection of a signaling server . The first lines of the code snippet opens a webSocket instance wss . Next line open the connection with port 8888 .
If any user is connected to the server , it will handle the connection request by listening handler . Finally the connection is closed with the connection close handler .
How to run the server ?
first of all you should ensure whether you are installed all node package . You can check the node version with node -v or node –version command . If your signaling server named as server.js , then you can run the server with node server.js command. If the server is success in execution , then you can see the message “server started” on the console .
If you are interested and you want to test with writing webRTC application from scratch , Just go though our posts , Write a WebRTC Application – Programming from scratch
- Part 1 – How to Create a Simple App Like A Pro
- Part 2 – Create a server with Node.js for the App
- Part 3 – Design your first WebRTC App
Conclusion
You will get an idea about how to write a basic signaling server through this post . Also you can get a overall idea about node.Js and webSocket too . If you have in any case a query or suggestion , you can write it on comment section of this post .
If this post is useful and informative, of course don’t forget to share our post or recommend this post to your friends
Leave a Reply