Larrikinism

It is the Confession, not the Priest that gives us absolution

Archive | RSS
November 1, 2010 at 6:16am
Home

node.js+ mogodb+ websockets

Now there’s an amazingly buzz-word laden title, if I ever saw one. Anyway, if you, like me, have been hearing more and more about of these terms floating around the interwebs in additions to things like ‘coffee script’, ‘nosql’, ‘event driven programming’, ‘document-oriented databases’ etc. etc. read on for a gentle introduction to new paradigms of programming, new server architectures and new persistence layers.

I’ve always found that the best way to get started with a new technology is to start writing code using it. Of course this has to be followed by very in depth reading about the topic, but it helps if you’ve actually done something to follow along with what people are writing. So in my quest to understand node.js, mongodb and HTML5 websockets, I chose to implement a very basic EtherPad( used in typewith.me ) clone.

The components:

node.js

From what I’d heard about it, its a blazingly fast web server written in JavaScript running on Google’s V8 engine. I still haven’t gotten to a stage where I can benchmark the code myself. But the benchmarks provided here looked really exciting. Installing node was a cinch on my ubuntu system. The usual:

          $ wget http://nodejs.org/dist/node-v0.1.99.tar.gz
          $ tar xfz node
-v0.1.99.tar.gz
          $
./configure
          $ make
          $ sudo make install

Brownie points to the node team for making the make tool give pretty output.

The exciting, game-changing thing about node.js is of course Evented I/O. The presentation below is the best explanation of the concept, that I’ve come across so far.

Evented I/O based web servers, explained using bunnies View more presentations from Simon Willison.

Coffee Script

Since I was going to be writing JavaScript, I thought I’d kill two birds with one stone and look into Coffee Script which helps you avoid JS’s ugly ugly syntax. I would definitely recommend investing the extra time learning this useful tool if you’re going to be developing for node.js or any JS library for that matter. Also as a bonus you can run node.js apps directly using coffee app.js.

express

In three words this is: Sinatra for JS. Being a big fan of the simplicity and transparency that sinatra offers, I pounced on this particular framework for my app. The framework for node.js scene had erupted recently with lots of exciting developments, including: A standard base for frameworks to be built on (similar to ruby’s rack): connect. Being developed by the fabulous guys behind ExtJS (now Sencha). Connect is still however in its infancy and does not have much support.

          get('/' ->
           
'Node says, Hello world!'  
         
)

kiwi

Pakage management for node.js libraries based on rubygems.

          $ kiwi install express
          $ kiwi list
          $ kiwi remove express
          etc
. etc.

I must add here that node.js is still at version 0.1.99 which is infancy compared to most software projects. And is by far not the only server to incorporate Event-driven I/O and to be written in JS(see the end of this article). It has however made sure that those idioms extremely popular and has fostered development along those lines in the ruby community and beyond as well. Think: EventMachine and all the em-* libraries.

mongodb

So I needed a persistence layer for the app and wanting to use a NoSQL DB. I went for mongo coz’ … well … how do you not look into something named mongo. Anyway mongo is a great place to start your jouney to the Dark Side of The Moon, the moon being the database implementations landscape.

To get started playing with mongo, sudo apt-get install mongodb. This gives you a rather limiting mongo REPL where you can try stuff out. When migrating from an RDBMS to mongo there are some changes in vocabulary that you will have to embrace. Tables are collections and rows/records are documents(hence document oriented db). I won’t go into a tutorial about mongodb as there are quite a few of those out there. But, I’ll try and cover some of the benefits.

  1. Dynamic Queries like:

    db.users.find({address: /[D|d]elhi/, age: $lt 25}).sort({name: 1})

That’s right, I just queried a db with a regex. And chained queries! Stuff, I could only do with an ORM that would ineffeciently translate that into SQL.

  1. Neat JSON-like language for queries called BSON

  2. Embed has-many associations.

          db.users.insert({
            name
: 'Ratan',
            notes
: [
             
{content: 'The Daily Show is awesome ...'},
             
{content: 'Jon Stewart is awesome ...'}
           
]
         
})

No unnecessary tables for holding strictly hierarchical data.

I’m sure there are many more but there are the ones that I encountered within 2 days of using it. Another thing that I should add here is the scalability promise of NoSQL DBs. Many users(me included) are drawn to NoSQL by its horizontal scalability. This article seems to debunk that, so i would advise further research before relying on that promise.

I’m not in the best position to debate the pros and cons of document-oriented DBs vs. the traditional RDBMS. But I do think I can propose a rule of thumb to decide between the two if your data fits well into either of the models:

 use NoSQL if (no. of objects central to your app)/(no. of small, supporting and dependent objects) >> 1  

websockets

One of the most awaited aspects of HTML5 this revolutionary protocol will make all the hackery featuring flashsockets and jssockets to create real-time web apps obsolete. Support is limited to only chrome and safari currently but that is because the spec for the protocol is not finalised yet. Firefox, waiting for the spec to be finalised, will support it in Firefox 4 and IE … well… First impressions of websockts are that its is extremely easy to use and will be one of the prime uses for node.js apps. Writing your server and client in JS gives the feeling of just on complete library connected by passing JSON messages to each other.

I used the extremely simple ws.js on the server side and jquery.ws.js on the client side. If you want a bit more power and flexibility, check out Socket.IO which comes with its own client library.


Hope this cleared the air about a couple of these web dev buzz words and encourages you to look further into it. While writing this article a tweet from DHH brought this to my attention. Clearly there is a lot out there beyond the technologies out lined here and there are just current-popular implementations of philosophies and ideas that have been around for a while.