Always grab your (web) clients attention! Using WebSockets with NodeJS

Always grab your (web) clients attention! Using WebSockets with NodeJS

In various applications it is possible that the customer may want to receive messages (or notifications, depending

How to use WebSockets with NodeJS? In several applications it is possible that the customer may want to receive messages (or notifications, depending on the case) from the server.

The first solution would be to do polling of these messages: to consult the server for notifications at certain time intervals. This scheme is simple, but has a problem that is not scalable (if there are more users, more and more queries go to the server, in which the vast majority probably have a null response).

Another solution is to use sockets for connection: the client application connects to the server, and it sends messages to you once you have them. This is the solution that we will analyze, over HTTP (which are called WebSockets) to be implemented with NodeJS as backend. The proposed solution allows for the implementation of WebSockets (native of HTML5) in the frontend.

Basic implementation NodeJS

We need the module ws, which can be installed using npm:
npm install –save ws
And in the code, you must first create a WebSocketServer:

var SocketServer = require('ws').Server;
var TheSocketServer = new SocketServer( { server: myServer} );

Where myServer is the object that represents the application server (obtained by doing the listen) .

Once this is done, it is only necessary to redefine the methods we need. In general, the messages received depend on the connection (i.e. not all clients are equal, nor do I deterministically always send them the same), so it is often usually good to define the on message directly on the client socket connection:

TheSocketServer.on('connection', function(client){
  //do stuff for connection

  // defining methods
  client.on('message', function(message){
    //do stuff on message
    if (responseIsNeeded){

  client.on('close', function(){
    // do stuff in case of disconnection
    console.log('Client has disconnected');

If we need to get values from the query requirement (ie ) in the connection ( or other method), you only need to:

TheSocketServer.on('connection', function(client){
  var url = client.upgradeReq.url;
  //En url tenemos la direccion entera, solo es necesario separar los parametros

This can be used so that the client, upon connecting, sends his-her ID or user name, to distinguish him from other customers from the start.

Make a Broadcast To All Customers

Sometimes messages receive answers (as in the case of request-reply), but other times you want to communicate something to all users, or those that meet a particular condition.
For example, there may be a type of client that creates notifications, and others who receive them. When the first sends a message (reaching the on-message on the server) , there should be a broadcast for all customers that receive notifications (this is the basic outline of a publisher-subscriber) .

The solution to doing this is similar to that shown above previously:

TheSocketServer.broadcast = function(message){
    TheSocketServer.clients.forEach(function each(client) {

In the example above, we should just call the broadcast function, from the on-message, with the received message.