Create your own freaky chat server in 10 minutes :)

This blog is how Node.JS helps to make a chat server in just 10 min time span. When this code first worked out at my end I felt awesome as in my past experience creating a chat server was cumbersome.  The algorithm for creating a chat server is simple

1.     Create a socket array which will contain all the sockets

2.     Each socket represent an user who wants to chat

3.     Server should open a port for listening

4.     Each socket chat message data to this port

5.     Once the data is received it is broadcasted to all the existing socket in the socket array.

Server side script

//Require net module for create socket connection

var net = require('net');

//Initialize the Socket array which will hold the current logged in used socket

var sockets = [];

var s = net.Server( function(socket) {

            //Whenever there is a data sent from any user, the data will be pushed to all the existing socket connections

            sockets.push (socket);

            socket.on('data', function(d){

                        for(var i=0;i<sockets.length; i++)





            //If a socket is null or gets disconnected from the existing pool, it should get removed else the application will crash, because it doesn’t know what to do with the data when there is a null socket.

            socket.on('end', function() {

                        var i = sockets.indexOf(socket);

                        sockets.splice(i, 1);



//Tell the server to listen to port 8080


Let’s start chatting

1.     First thing to do is know your ip address which will be required for future telnet connection
      2.   Open Git and compile the above script


3.     Now we need to give the ip addresses to the clients who want to chat. They need to do a telnet connection at the specified port

telnet <ip-address> 8080

Once done, they are good to go with the chat

In the above screenshot I have shown three consoles which are connected with the server. I have deliberately kept a bug in this code, which is , whoever will type the characters typed by him/her will be replicated twice. The first left top console starts the chat with right console. Left bottom console joins after some time. All the three users chat with each other and the message is broadcasted to the other. 

Cool! I am just wondering if I can make this happen in Azure Mobile Services. It will be an awesomely easy chat based application that we can build.


Popular posts from this blog

Firebase authentication with Ionic creator

Big Data - SWOT Analysis

LINKEDIN api call using NODE.JS OAUTH module