• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
  • Skip to footer

Engineering Semester

Explore the technology

  • Home
  • About
  • Contact us

WebRTC programming Part 2 – Create a server with Node.js for the App

By : VINAY KP Filed Under: application server, linux, NodeJS, webRTC 2 Comments

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 .

Download Node.js package 

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 !!!

Recommended Posts

Default ThumbnailImportance of Application server in Web technology Default ThumbnailWebRTC programming Part 1 – How to Create a Simple App Like A Pro Default ThumbnailWhy you need a signaling server in WebRTC application – You can think it
Previous: WebRTC programming Part 1 – How to Create a Simple App Like A Pro
Next: WebRTC programming Part 3 – Design your first WebRTC App

About the Author

Vinay KP is the founder of Engineering Semester blog . He is currently working as a software engineer . He is a very passionate blogger with Interest in research oriented technical articles .

You can contact via

Reader Interactions

Comments

  1. KHDev says

    October 12, 2021 at 6:17 pm

    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

    Reply
    • VINAY KP says

      October 18, 2021 at 10:05 pm

      Yes. In order to run Web-socket, you must install WS package using NPM manager in NodeJS. Thanks for the comment !!

      Reply

Leave a Reply Cancel reply

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

Primary Sidebar

Latest post

  • WebRTC programming Part 3 – Design your first WebRTC App
  • WebRTC programming Part 2 – Create a server with Node.js for the App
  • WebRTC programming Part 1 – How to Create a Simple App Like A Pro
  • Why you need a signaling server in WebRTC application – You can think it
  • what is adaptive streaming and how it works on video player ?

Footer

About Admin

Welcome to Engineering Semester. I'm Vinay KP. founder and brain behind of this blog. You can feel free to contact me via





Categories

  • application server (4)
  • HTML5 (1)
  • Javascript (1)
  • kinect (1)
  • linux (2)
  • microsoft (1)
  • networking (12)
  • NodeJS (1)
  • streaming (3)
  • web technology (4)
  • webRTC (12)
  • windows (1)
Home | Contact us
Copyright © 2025 engineeringsemester