Saturday, August 21, 2010

NodeJS Based IM server


Simple and fast messaging over web sockets.


I recently developed an AJAX-based messaging client for a large social networking type website I am developing for a client at Rain. It works fine, but using AJAX to constantly poll the server is slow and creates a fair amount of overhead for the browser. Using web-sockets with a flash back-up for older browsers combined with NodeJS on the server side should create fast and simple solution for my messaging problem. It would also make it easier to separate the messaging server from the web application server to increase scalability.


After getting lost in a la-la land of non-supported NodeJS 1.x based web socket and chat clients, I stumbled on Socket.IO, a library that contains both front-end and back-end code for delivering high speed chat over web sockets using NodeJS 2.x.

Getting Started

Following these simple steps in terminal got me a working chat server up and running in about 5 minutes.

# install node
cd ~/dev;
git clone git://;
cd node;
sudo make install;

# install socket io
cd ~/dev;
git clone git://;

# start socket io
cd Socket.IO-node/example;
node server.js;
cd ~/dev;

# install the socket io web client
git clone;

# copy the javascript over to the example directory
cp Socket.IO/ Socket.IO-node/example/client/;

Witness the awesomeness here: http://localhost:8080/chat.html

Moving forward

I love how easy it is to get started with NodeJS and Socket.IO. I intend to put it to use to build my messaging client. One key feature that might provide a problem is logging the messages. Currently Ithey are being stored in MySQL on a separate database server. Because the storage doesn't need to be real time, only the messaging, I could continue using AJAX to log messages. I might also be able to hook into MySQL directly from the server in a way that does not slow down communication. More research will be needed to determine how practical an approach this will be and how it competes with AJAX/PHP on resources, scalability, and browser support. At the moment I'm excited about the potential and hope to write more about it if I get it up and running!

Resources - Server-side Javascript engine. - A great module for NodeJS that adds support for web-socket communications.  - A good tutorial on how to get started with Socket IO for NodeJS.

1 comment:

  1. thanks for the post, nice intro to node.js and i am planning to use it for twitter-like realtime status update with zend framework, still learning more about them