Wednesday, August 12, 2015

Websockets in node.js

In this post I'll show you how to start using websockets in node.js, the different types of modules there are, how to install them and use them to develop a simple example.

For those who don't know what websockets is about, here is a brief introduction:

Wikipedia says "WebSocket is a protocol providing full-duplex communications channels over a single TCP connection", this means that in a client-server communication the client as the server can send a message leaving behind the request-response protocol, where the exchange is always initiated by the client (usually a browser) and the server cannot send any data without the client requesting it first.

It also says "WebSocket is designed to be implemented in web browsers and web servers, but it can be used by any client or server application", websockets was introduced in the clients or browsers since the html5 specification.

If you want to know more about websockets refer to the following links:


Let's start with the first module "WS" , to start using it it needs to be installed so the following commands install the library:

To install the module to the current directory use the following command:
     $ npm install ws

To install the module in a global way use the following command:
    $ npm install ws -g

Once install it we can create an example using this module, the code below shows how to create a simple server with ws:

Save the file as ws_server.js, from the code you can see that the server will be running in port 8080, any request to this port the server will attend it. To create a client use the code below:

Save the file as ws_client.js and now lets run them, to run the server just type the following commands:

   $ node ws_server.js
   Msg received in server: Msg from client

   $ node ws_client.js
   Msg received in client: Msg from server

From the output you can see that the server starts and listens to any message when the client starts it also listens to messages and it sends a message, then the server receives it and answers this message

To find more about ws module go the following link  is the page of the library.


Lets try another module for websocket, the WebSocket-Node module, this module has more requirements, needs c++ and python to compile the native modules but they are optional but its efficiency decreases if they are not installed, if you are a windows user you will find harder to install.

To install WebSocket-Node module use the following command and use -g option to install it globally:

    $ npm install websocket -g

Once installed the module lets proceed to create the example:

Save the file as websocket_server.js. It creates a server for websockets that listens requests at the url ws://localhost:8080/example, in the code you can see that the port is configured at the instruction server.listen(8080), and the path is formed with request.accept('example', request.origin); .

Let's create a client for the server:

Save the file as websocket_client.js, from the code you can see that the client creates a connection to the server using the instruction client.connect('ws://localhost:8080/', 'example'); ,when a connection is done a message is sent to the server and the server should respond to the client with also a message.

Now lets run the scripts using the following commands:

   $ node websocket_server.js
   Received Message: Message from client

   $ node websocket_client.js
   Received: 'Msg from server'

The messages that the server and client are sending should be printed in the console if everything works as expected, if a problem happens with  the connection you should see a message like this "Connection Error: Error: connect ECONNREFUSED".

To find more about websocket module go the following link  is the page of the library.

Another module to use for creating websockets is, although this module is part of a bigger framework you can still use this module to create websockets.

To install the module use the following command, to set it globally use the -g option:
    $ npm install -g

To create a client for this module the module "" is required, lets install the module:
    $ npm install

Lets create an example of a server:

Save the file as engineio_server.js, from the code you can see the configuration needed to run the server, it is pretty simple.

Now lets create a client for this server:

Save the file as engineio_client.js and lets run the example with the following commands:

        $ node engineio_server.js
         Msg Received at server:Msg from client

       $ node engineio_client.js
        Msg Received at client:Msg from server

As in the others examples if everything works as expected you should see the messages exchanged by the server and the client in console.

To find more about module go the following link  is the page of the library.

There are many other libraries that can be used to create websockets with nodejs, every one with its pros and cons but it would take another post to discuss this topic. I guess the hardest part would be to choose which library to use.

I wish this post helped you to better understand what websocket is and how to implement it in nodejs.


  1. Nice Information you have written here. Really Great Stuff. I keep it bookmark for our future purpose. Our Best Presence in Content Marketing Find our Content Writing Services in Nagpur & Content Writing Company in Nagpur .
    AceZed IT Solution

    1. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging. If anyone wants to become a Front end developer Node js Training in Chennai . learn from or Javascript Online Training from India. Nowadays JavaScript has tons of job opportunities on various vertical industry. JavaScript Training in Chennai

  2. very very amazing explaintion....many things gather about yourself...yes realy i enjoy it
    Digital Marketing company in Chennai

  3. I’m sincerely suggesting your blog to all my friends… I’ve changed myself in many thing after reading your blog… Thanks and keep going.
    J2EE Training in Chennai
    Java Training in Chennai
    Linux Training in Chennai

  4. Thanks for this useful information about Nodejs. This article is really helpful for learning. Nodejs Training in Bangalore

  5. Awesome blog. It was very informative. I would like to appreciate you. Keep updated like this best sap simple finance online training institute in hyderabad

  6. I appreciate your efforts because it conveys the message of what you are trying to say. It's a great skill to make even the person who doesn't know about the subject could able to understand the subject . Your blogs are understandable and also elaborately described. I hope to read more and more interesting articles from your blog. All the best.
    python training in rajajinagar
    Python training in bangalore
    Python training in usa

  7. Its really an Excellent post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog. Thanks for sharing....
    excel advanced excel training in bangalore
    Devops Training in Chennai

  8. Hello! This is my first visit to your blog! We are a team of volunteers and starting a new initiative in a community in the same niche. Your blog provided us useful information to work on. You have done an outstanding job.

    AWS Training in Marathahalli | Best AWS Training in Bangalore
    Amazon Web Services Training in Chennai | No.1 AWS Training for Solution Architect in Chennai

  9. This is ansuperior writing service point that doesn't always sink in within the context of the classroom. In the first superior writing service paragraph you either hook the reader's interest or lose it. Of course your teacher, who's getting paid to teach you how to write an good essay, 

    Java training in Pune

    Java interview questions and answers

    Java training in Chennai | Java training institute in Chennai | Java course in Chennai

    Java training in Bangalore | Java training institute in Bangalore | Java course in Bangalore

  10. This is very good content you share on this blog. it's very informative and provide me future related information.
    python course institute in bangalore
    python Course in bangalore
    python training institute in bangalore