Getting started with Node.js and Web Sockets on Ubuntu 10.04

Having spent the past few hours taking my first steps into the glorious unknown world of web sockets I’ve drawn a few conclusions about web sockets:

Screenshot of Node.js running Web Socket Server on Ubuntu

So this post I want to document a simple demonstration of the use of web sockets with node.js  which works now (August 2010) to serve as simple demonstration to those wanting to dip their toe in the water before going further. Many examples and demos were outdated and failed to run for a variety of reasons, mainly due to node.js being updated and the examples not being changed to reflect this. I did all this on ubuntu but there is no reason why it shouldn’t work on any other Linux system.

Requirements

For this demonstration you will need:

  1. A computer running a recent version of linux
  2. A web socket compatible browser (probably chrome/chromium v4 plus)
  3. Appropriate compilers (Obtained on ubuntu/debian using apt-get install build-essential)
  4. Git (Obtained on ubuntu/debian using apt-get install git-core)
  5. A basic idea about compiling things, some basic JavaScript or a healthy sense of adventure!

Getting Node.js

Node.js is an event driven JavaScript framework for building server side applications such as web servers and web socket servers. A large proportion of web socket examples use it as a base for the server component of the system so the first thing we need to do is get it and compile it:

  1. Get the latest code by cloning the git repo: [sourcecode language="bash"]git clone git://github.com/ry/node.git[/sourcecode] This will get the code for node.js and place it in a folder called node.
  2. Now we've got the code we need to compile it which should go smoothly:
    cd node
    ./configure
    make
    sudo make install
    Now, assuming everything went well node.js will be launched if we type node in the terminal, if compiling hasn't worked it's probably worth looking at the output from the ./configure command as there may be some missing dependencies....I was fine though!
  3. When required it can be invoked by [sourcecode language="bash"]node <script>[/sourcecode]

In itself node.js is not very useful without some javascript to kick it into action, thankfully many people have put together implementations of web socket servers using node.js.

Getting a Web Socket Server

This is the area where I had most trouble, I found that many people have written similar posts to this and they had quickly become dated and broken, mine may indeed follow them but at least for the present will remain working.

The best simple web socket library with examples I came accross was miksago’s node-websocket-server, it seemed to fill all my criteria of being simple, up to date, well maintained and including working examples. The main example being a server which sits and waits for the included client page to connect and then prints a message to the terminal, the server then sends a message back to the web page showing the id of the client. Useless but very understandable code upon which to base a simple application. To get get this:

  1. Again we start by cloning the git repo:http://github.com/miksago/node-websocket-server [sourcecode language="bash"]git clone http://github.com/miksago/node-websocket-server.git[/sourcecode]
  2. Then change to the new directory and go into the example folder
  3. Launch the server :[sourcecode language="bash"]node echo-server.js[/sourcecode] This will start the server listening on port 8000.
  4. Launch the client.html file in your browser...from there on it should be self explanatory and you should be able to send messages through web sockets.

Note: If you’re running the server on a separate machine you’ll need to change the host name in the client.html file.

Moving On

Once you’ve used this demo and looked at the code, I found at least, that it becomes clear how simple web sockets are to implement. When you go on to create your own applications this example provides a good starting point.