Up to now, this chapter 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.
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:
$ sudo apt-get update $ sudo apt-get install nodejs
$ node -v
$ wget https://github.com/rauchg/chat-example/archive/master.zip
chat-sample-master
:$ unzip master.zip
$ cd chat-sample-master
$ npm install
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
3000
. Now you can access the app at http://server-ip:3000
. The screen will look similar to the following image: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.
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