Welcome again to WebRTC APP tutorial series. This is the continuation of WebRTC App tutorial. Now we are pretty clear what needs to be implemented and what is our design. In this post, I am going to create a server with Node.js for our application.
If you are new to this programming series, please get some idea about first webRTC application and it’s design . You can also check those previous post about “What is webRTC and it’s components” .
Introduction to webRTC
Why we need signaling server in WebRTC
Now let’s start to create a server for our webRTC App.
What software are required to be installed before the code ?
We are going to use Node.js for our server . Now let’s look how to install Node.js in your system .You can download the Node.js package from Node.js official site for all the Mac , Windows and linux users .
What is Node.js ?
Node.js is a tool for creating a fast networking application . It’s a complete javascript package . The one of advantage of Node.js is that they provide lot of useful libraries via NPM . NPM is a package manager for Node.js . You can install Node.js libraries via NPM manager using the terminal .
To ensure Node.js has been installed in your system , you can check the version by running following command in terminal .
Tes Node version : node -v Test NPM version : npm -v
Now we got Node.js for your application . We are ready to start to write our first application server with Node.js . We are going to use javascript as the programming language (Node.js comes with javascript) for our server side code .
Web Socket connection with Node.js
Note : I hope you are already installed Node.js and NPM packages in your system . I have already mentioned that NPM is a package manager which help us to install required libraries/API’s for Node.js
In our first webRTC application , We are going to use Web-socket API in Node.js to create a connection . You can manually install web socket in Node.js with the help of NPM.
npm install ws
Note: To understand more about ‘ws’ package ws-package
Next let us write a socket connection with web socket in Node.js for connecting the application with user . Create a javascript file for example server.js .
var WebSocketServer = require('ws').Server; var wss = new WebSocketServer({ port: 8888 }); var users = {}; wss.on('listening', function () { console.log("Server started with port 8888"); }); wss.on('connection', function (connection) { console.log("User connected"); /*Action to do when user send messages */ connection.on('message', function (message) { console.log("message from user"); }); /*Action to do When user try to close the connection*/ connection.on('close', function () { console.log("Disconnecting user"); }); });
This is the basic skeleton of a Node.js server with Web socket . We have used port 8888 for the connection . You can use any port for the connection and check whether the server is running . Later we will add some logic in the server code to handle the cases of new user is connected , user send a message and user quit from our application .
How to run our application server with Node.js ?
Now we are familiar with the basic skeleton of a Node.js server with Web socket . This is the time for testing the server . You can easily run the server by typing following command in terminal
node server.js
This is very much easy . Isn’t ? Node.js is very powerful , user friendly and easy to use .
Conclusion
That’s all for this tutorial . I hope you have understood about the importance of server in our application . This is the time for experiment .Go and write your first server with powerful Node.js Web socket . We can meet with next post for our first webRTC Application . See you again !!!
KHDev says
Good morning,
Thank’s for your explainations, but :
when typing ; node server.js
It gave an error :
cannot find module ‘ws’
in order to resolve this error you must :
install ws module : npm install ws instead of : npm install websocket
VINAY KP says
Yes. In order to run Web-socket, you must install WS package using NPM manager in NodeJS. Thanks for the comment !!