I've been looking for a small hosting provider that I could run node.js on to test out some WebSocket experiments, and this is my account so that you also can have a play.

I should add before I continue, that it's not difficult by any measure if you know what you're doing (which I was about 80% of the way there myself) as per:

@rem since your last tweet (roughly 20 minutes) I bought a slice on Slicehost for 20$ and got the #nodejs example server running on it.

Slicehost

I did try out Rackspace Cloud at $11/month to start off with, but after the registration was complete they told me that they needed to call me to finalise verification. I wasn't at home so I didn't get the call. Plus, since I've moved my bank account is in a weird flux at the moment. Suffice to say, the account was never completed and I couldn't get access to the server. So I cancelled quickly and looked at registering with Slicehost.

Slicehost registration was a breeze - I started with the smallest option. One form to complete and the server was up. Amazingly easy. They had preselected an "Ubuntu 8.04.2 LTS (hardy)" distro for me, which if I was asked to select one of the distros myself, I'd have no idea which would be a decent starting point otherwise: 8, 9, 10? Who knows, I just wanted to get going. Perfect.

Immediately my server was running. I did hit one security block which you're unlikely to also hit because I was registering from the UK, but accessing from Switzerland (since I was still on holiday), but the customer support notified me of the security issue (which blocked my server) and literally had it all sort and resolved within 10 minutes. I whole heartily praise their support for the speed and personal touch they had sorting this out.

So, getting set up. I'd need the following:

  1. My own account, rather than root
  2. Git (so I could clone the repos)
  3. Node.js (and thus also c++ compilers, etc)
  4. WebSocket server

Configuring the Ubuntu server

Slicehost has a superb wiki with instructions here:

Getting Started With Your New Ubuntu Slice

I followed the directions up to the "pimping out your prompt" (since I've got my own poison). That was the user config done and now I could sudo and root was blocked.

Installing Git

Git was simple, although I did go round the houses a little. Just run:

sudo apt-get install git-core

Now git was installed.

Installing Compilers & Node.js

Getting node was simple as all I needed to do was clone the repo:

git clone git://github.com/ry/node.git

The problem was actually building Node - I hadn't installed the compilers, c++, cc, gcc, etc. Using a combination of hit and miss and shotgun approach, this was also simple:

sudo aptitude install build-essential -y

This gave me all the compilers, so I was then able to build.

I'm now going to fast forward through my experience and rather than take you round the houses, just tell you that you want the stable build and not the latest. The WebSockets don't work properly with the latest 0.1.28 build, and the stable build is on 0.1.26. So you need to switch branches (from the node directory):

git checkout origin/stable

Now you're ready to build. It actually took me a lot of faffing around to work out I wasn't on the stable release, so consider this a favour! :) Now we're ready to build:

./configure
make
sudo make install

Next stop, let's get the WebSocket server running

WebSocket Server

I personally wanted to play with the streaming Twitter API, conveniently there's a git repo for that!

I created a web directory in my home directory so that I could run my experiments out of, from there I run:

git clone git://github.com/andregoncalves/twitter-nodejs-websocket.git

From there I kicked off the server (by default it listens for "iphone"):

cd twitter-nodejs-websocket
node server.js <username> <password>

Now on my other "normal" server (though the smoothness of slicehost, I'm now thinking of porting across all my sites) hosts a simple web page (from the example in the git repo) that streams the latest tweets found by the server and should be viewed on a WebSocket enabled browser, i.e. Google Chrome (dev channel) or Webkit nightly: http://rem.io/relay-tweets.html (note that I'll be running this demo for a while, but it may well be taken down in the future). Here's a screenshot in case I've shut it down and moved on:

Screenshot of streaming tweets

The core code to run the socket is simply this:

ws = new WebSocket("ws://<host>:8080/");

ws.onmessage = function(event) {
  var data = JSON.parse(event.data);
  var $p = $(twitterlib.render(data)); // uses Twitterlib.js
  if ($('#tweets > li').length > 15) {
    $('#tweets >li:last').slideDown(100, function () {
      $(this).remove();
    });
  }

  $('#tweets').prepend($p);
  $p.slideDown(140);
};

ws.onclose = function() {
  alert("socket closed");
};

No doubt I'll be posting an article on HTML5 Doctor about WebSockets and writing about it in the upcoming Introducing HTML5 that I'm writing with Bruce Lawson.