Message
back to the journal

Nodejs. I just have to use it for something.

February 15th, 2010

nodejs.org

A little while ago, inspired by Simon Willison’s demo of Nodejs at the FullFrontal conference, I felt the urge to make something, anything, using Nodejs.

Nodejs gives lightening fast, event driven IO with Javascript running server-side in the rather nippy V8 Javascript engine. Until Nodejs, I hadn’t seen much need to bring Javascript to the server other than for testing and as a bit of a novelty.

Nodejs changed all of that. Built by Ryah Dahl with a strict philosophy that none of its procedures should ever perform a blocking operation, it has a single threaded architecture and relies heavily on the use of a single event loop. Anyone with a good knowledge of Javascript or even just a familiarity with jQuery will be familiar with the pattern of event handlers, callbacks and closures needed to build some really rather powerful things, quickly and easily with Nodejs.

For my part, I just wanted to make something to explore the ease in which Nodejs could support comet-style long polling for some real-time, collaborative task. The Web is already getting flooded with examples of chat rooms and IRC servers built with Nodejs, but I really wanted to make something that felt like a real web application, not an example.

To that end, I resurrected an old idea which I first built circa 1999 for randomly choosing someone from a list to make the tea. Not earth-shattering, but a bit of fun and simple enough for me to see through to completion. For this incarnation, dubbed Teafr.com, I piggybacked on the lists feature of Twitter to allow people to create and manage their lists of potential tea-makers somewhere they are already comfortable. My app would allow a user to recall a list of their choosing and then perform a lottery, selecting a winner (or loser) from the list at random. To add a little suspense (in the loosest possible sense) I added a basic animation which gradually got slower until the winner was revealed.

teafr.com

I captured a couple of videos showing teafr.com doing its stuff, one of them on an iPhone.

The fun part of this for me was in allowing this lottery and the animation to be visible to everyone viewing the same list from wherever they are. Nodejs makes it trivially simple to handle long-lived http requests so that all clients could listen out for the initiation of a new lottery event. Since this is all just lightweight HTTP, HTML, CSS and Javascript, it also runs pretty nicely on both my iPhone and Palm Pre. It is rather satisfying to stand next to someone who is viewing the site on a desktop machine, and then start their screen animating with a quick tap on your handset.

Cheap thrills, I know.

It seems to me though, that the cheapness of the thrills is what is so exciting. I’m no rocket scientist, and I managed to create this entire application, complete with real-time online views in a couple of evenings work. The short hop and low cognitive friction for a Javascript developer to be able to build these kind of things from end to end is incredibly liberating. I’m bursting at the seams with ideas for things I want to build, and am now capable of building with a new, powerful and yet familiar tool set.

The only detail which kept my new toy from the masses was having a suitable place to host it. I’ve been using Dreamhost for a while now and have to say that I’m pretty happy with them, but even with the great level of control and access to your server that they give you, you need a little bit more control to compile and run Nodejs. The easiest and cheapest way that I found to host Nodejs powered site was with a dedicated virtual server. There is no shortage of providers in this space and as Web developers get more sophisticated, the use of this kind of solution is getting more commonplace. I opted for Linode which is working out rather nicely for me, but equally, I could have plumped for a similar offering from Slicehost or Amazon Web Services. I’ll soon migrate all of my sites onto the single Linux instance that they host for me.

For now though, I need to resist the temptation to spend all of my spare time (ha!) noodling away with Nodejs, MongoDB, jQuery and various other fun things which ’speak Javascript’ that smart people on the Web keep on creating and selflessly sharing with the world for free.

11 Responses to “Nodejs. I just have to use it for something.”

  1. san1t1,

    First thing I built using node was a little pub server that receives http notifications (of phone call events), parses them, and then forwards them to interested clients over either an ordinary TCP socket or over a WebSocket. I didn’t need persistence or “proper” pub sub, and node was simply the easiest thing to do this in. Oh, and I can tail the logs it generates through WebSockets too…

    This in ~250 LoC. I built exactly the same app in C# and it took more like ~2000 LoC.

    Both of these required implementing a WebSocket server.

    To be fair, the C#/mono variant will run on more than one processor…

  2. FND,

    It will be interesting to see whether/how the JavaScript community will build a standard library - that seems like an obvious,and crucial, development at this point (CommonJS is a great step in the right direction).

    If only JavaScript’s syntax wasn’t so verbose… (Yes, I’m a spoiled Pythonist.) Although for server-side code, I might have less objections to using an alternative syntax which is then compiled to JavaScript.

  3. FND,

    I forgot the most important part: Where’s the source? It’d be great if you could put that up on GitHub.

  4. andrew,

    webfaction was a piece of cacke took less time to setup on there than my ubuntu in vmware

  5. Links London,

    http://www.linkslondonsale.com/ I forgot the most important part: Where’s the source? It’d be great if you could put that up on GitHub.

  6. Links of Londonhttp://www.linkslondonsale.com/,

    webfaction was a piece of cacke took less time to setup on there than my ubuntu in vmware

  7. Links London,

    http://www.linkslondonsale.com/ If only JavaScript’s syntax wasn’t so verbose… (Yes, I’m a spoiled Pythonist.) Although for server-side code, I might have less objections to using an alternative syntax which is then compiled to JavaScript.

  8. Kuroki Kaze,

    I recently built web-crawler on node.js (using HTTP Client + libxmljs). Source is not available now, but I plan to release it on Github at some time.

  9. Stuart Coyle,

    I’ve been experimenting with the same things. For those that baulk at Javascript’s hairy syntax I’ve found Coffee Script fixes that. Now I can write javascript and be as happy as if I were writing Ruby code.

  10. cha0s,

    Hey there, cool article ;)

    Just discovered node.js myself, and I’m also on DreamHost. I actually was able to get node.js up and running incredibly simply… what issues did you have? Feel free to e-mail me about it.

  11. NodeJS « Newsicare,

    […] NodeJS is just getting hot. Many programmers who used PHP, JAVA, Ruby on Rails are now switching to nodeJS 1, 2. […]

Post a comment





Submit your comment

WordPressRSSXHTMLCSS