Slicehost, Node.js & WebSockets

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.im/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.

8 Responses to “Slicehost, Node.js & WebSockets”

  1. You’ll probably want to add the node server as a service as well – since otherwise it will die when you close your SSH-connection or at least when the server is restarted for whatever reason.

    I managed to get it up as a service on my Ubuntu Slice with the help of this post: http://kevin.vanzonneveld.net/techblog/article/run_nodejs_as_a_service_on_ubuntu_karmic/

    I did some changes though and thinking about it I should probably write a blog post about it. Until then – my upstart script looks like:


    description “node.js importer server”
    author “voxpelli”

    start on runlevel [2345]
    stop on runlevel [!2345]

    chdir /var/www/node-site

    exec /usr/local/bin/node /var/www/node-site/script.js 2>&1 >> /var/log/node-site.log

    Change /var/www/node-site to the directory that you put your script in. I’m currently due to laziness running it as root as well – so you probably should change to sudoing node as well, as the post I linked to suggests.

  2. Hey Remy,

    I know that this is probably a bit late given that you’re all set up with slicehost now, but Linode (http://www.linode.com) do something very similar in the VPS space, but with more ram/bang for your buck and (the killer feature for me) a UK data centre as a no-cost option which = no more transatlantic latency lameness for us brits. The docs are a little bit sparser than slicehost’s, but the support and community are pretty great.

    Anyway, cheers for the walkthrough – time for a sunday night play with WebSockets methinks…

  3. @Pelle – ah, yes, I did actually plan to run the node server in a screen, but when I did I accidentally terminated it at the same time! It should be running again now, but you’re right, I should run it as a service.

    @Tom – cheers for that, I didn’t know linode do a UK option – that’s definitely worth knowing.

  4. Thanks to @jgeiger for forking the original from @andregoncalves and updating for the recent API changes in node.js – http://github.com/jgeiger/twitter-nodejs-websocket

    You can now run this on the latest node.js code. Yay!

  5. hey remy, were there any other reasons for chosing slicehost over rackspace?
    rackspace is damn damn cheap :)
    thanks,
    martin

    (answer via twitter if you like to)

  6. Hey Remy,

    Try out this js lib for Node that implements fall backs for all browsers to emulate comet style communication.

    http://github.com/LearnBoost/Socket.IO

    http://twitter.com/francoislaberge

  7. +1 for Linode. They’re excellent.

  8. Why not make it even easier and go with a hosting provider that is purly for node.js app. Check out http://bejes.us/ it’s running the NodesterJS platform.
    It even has an npm CLI tool now called bejesus-cli.

Leave a Reply
Not required

CODE: Please escape code and wrap in <pre><code>, doing so will automatically syntax highlight