24/7/365 Support

Chat server with Node.js on Ubuntu

Up to now, this article has covered XMPP and its usages. It is a good, mature protocol with multiple servers developed around it. Sometimes, however, you may need to set up a quick application that uses a simple message transfer, or develop a small chat application for your team. For such projects, XMPP servers may turn out to be overkill. You may not use all the features of XMPP and waste resources, even for a basic setup. Plus, developing an XMPP application is a time consuming process.

In this case, you can quickly start using Node.js-based socket communication. Node.js has gained popularity in the developer community. It is a framework developed in a commonly known language, JavaScript. In this recipe, we will learn how to develop a message passing application using Node.js sockets. We will use Socket.io, a popular Node.js library, to work with sockets and a demo app provided by Socket.io.

Getting ready

You will need access to a root account or an account with sudo privileges.

How to do it…

We are going to set up a Node.js-based application, so we need to install Node.js on our Ubuntu server.

Follow these steps to install Node.js:

Install Node.js with the following command:

$ sudo apt-get update

$ sudo apt-get install nodejs

Optionally, check your Node.js version:

$ node -v

Next, download the sample application from the Socket.io GitHub repo:

$ wget https://github.com/rauchg/chat- example/archive/master.zip

Unzip the downloaded contents. This will create a new directory named chat-sample-master:

$ unzip master.zip

Change the path to the newly created directory:

$ cd chat-sample-master

Next, we will need to install the dependencies for this sample application. Use the following Node.js command to install all dependencies.

$ npm install

This will fetch all dependencies and install them in the node_modules directory under chat-sample-master. Once the install command completes, you can start your application with the following command:

$ node index.js

ubuntu: ~/chat-example-master $ node index.js listening on *:3000

This will start an inbuilt HTTP server and set it to listen on default port 3000. Now you can access the app at http://server-ip:3000. The screen will look similar to the following image:

Open another instance in a separate browser window and start sending your messages.

How it works…

We have set up a very simple application that listens on a given Node.js socket. To send a message, we have used the socket.emit() function, which writes the data from text box to socket:

$('form').submit(function(){

socket.emit('chat message', $('#m').val());

...

});

When this message is received on the server side, the server writes it to all connected sockets, resulting in a group chat scenario:

io.on('connection', function(socket){

socket.on('chat message', function(msg){

io.emit('chat message', msg);

});

});

Similarly, to receive a message, we keep listening on the socket, and when an event chat message happens, we write the received data to an HTML page as a message:

socket.on('chat message', function(msg){

$('#messages').append($('<li>').text(msg));

});

This is very basic application and can be extended easily to implement one-on-one chat. All we need is a unique ID for all clients and a little modification to the interface to separate messages. Right now, the message is sent as it is; you can collect the message and create a JSON object to contain sender and receiver IDs, plus any additional information.

The advantage of using NodeJS is quick and easy development. JavaScript is a commonly used language and you can easily get support from the large community. You can always develop the application as per your requirements. The disadvantage is regarding scaling; you will need to code the clustering mechanism on your own, whereas for XMPP, clustering is implemented by nearly all leading servers.

There's more…

The Node.js setup available with the Ubuntu repository is not the latest one. You can download the latest version from the node official download page.

Download NodeJS binaries for Linux. Choose your desired version by visiting the NodeJS download page. As of writing this, the latest stable version is 5.1:

$ wget https://nodejs.org/download/release/v5.1.0/node-v5.1.0-linux- x64.tar.xz

Extract binaries and move it to /use/local so that it is accessible globally:

$ tar Jxv --strip=1 -C /usr/local/

Check the node version with the following command:

$ node -v

See also

Node.js download page: https://nodejs.org/en/download

Node: how to install: https://github.com/nodejs/help/issues/41

Sample chat application on GitHub: https://github.com/rauchg/chat-example

Help Category:

What Our Clients Say