« Back to blog

Goodbye Rails, Hello Node.js

A few months ago, I decided to play with node.js. The new server side JavaScript hotness and now I am hooked onto it. I use Ruby on Rails for most of my work. Though I am not a big fan of Rails, it is better than most things around and it's in Ruby which I really like.

Off-late most projects that I have been working on, have much less ruby and a lot more of JavaScript. My typical application consists of thin RESTful API transporting data as JSON, which is consumed by JavaScript in the browser. Can we not use JavaScript for the whole application instead? And get significant other benefits as well?

This is where Node.js fits in. Sitting on top of the highly performant Google V8 JavaScript engine, it gives you a bunch of libraries to write your networking code. In other words, it is JavaScript on the server side. FTW

As a side note, I used to be extremely scared of the JavaScript funkiness, till I apprenticed with some JavaScript masters. Over a period of time working on more and more JavaScript, I have gotten pretty proficient. I still don't have the same control with JavaScript as I have on Ruby and  I still forget to add the semicolons to the statements, but it's not that often now.

Asynchronous event driven API that leads to high scaling and lower memory footprint, we all know that (The now famous C10K problem). I have used EventMachine in Ruby and it always felt like working against the grain in ruby. Blocks typically are thrown for instant execution and rarely as a callback. The lack of Fibers in Ruby 1.8.7 does not help. Turning non-evented ruby code into evented ones is mind-bending exercise till you get it.

On Node.js it's the most natural way of programming, it doesn't even feel like a shift in style. JavaScript programming is all about callbacks invoked later in time. There is no such thing as EM.defer and EM.run, and checking if you are in a reactor loop.

There is some serious innovation happening around Node.js. Some are being built over experiences of other Languages. There is no such thing as Rails for node.js (which I consider a smashing thing). Most people use Express which is similar to Sinatra. Coffee script became popular because of and in the Node.js community. Now the Rails community is trying to make coffee script the default . RVM became popular only in recent times in the Ruby Community, We already have NVM doing the same thing for node versions. I can't forget to mention the fantastic Socket.io for realtime push applications and it is truly trivial to use.

The mushrooming of cloud hosting for node.js is also a proof that a lot of vendors think it's interesting. Just off the top of my head, there is Joyent's no.de, Cloudfoundry, Duostack and Nodester all in some sort of beta stage. Although from my brief experiments, I prefer the CloudFoundry to no.de and Duostack.

And Rails definitely does not have a rap theme song as cool as this. http://soundcloud.com/marak/marak-the-node-js-rap. (Listen with headphones, please)

 

 

Posted April 21, 2011 by sreeix 
Apr 21, 2011
sreeix liked this post.
Apr 21, 2011
vishnu said...
hmm ok. I don't know. I still feel I can get stuff done faster with rails. I might eventually face a performance problem I guess, but that's far enough in the future that it may not be a big deal. The whole fibers + Eventmachine thing looks pretty promising too.

the really big deal I think would be the sharing code between browser and server I think.

Apr 21, 2011
sreeix said...
Hi Vishnu,
I find Rails way too complex to do simple things. Larger well defined apps i'd definitely would think twice before using node.js. A lot of patterns still have to be figured out.
Also it's not simple go get off the rails and use couchdb/redis on your rails server, as i found out in one of my rails gigs. While I had a redis backed node.js app up and running in an hour and in production in another. YMMV

I find EventMachine + fibers hard to implement, but harder to understand after a few days. As ruby dev i expect my block to be called right away. On the other hand as a javascript dev i totally understand the evented nature of it. Maybe i will get used to it. It still is a big deal to turn a simple rails app to be completely evented.

Apr 22, 2011
vishnu said...
You may be right about the simple things. I don't really know what you mean by that though. I mean I would assume if its simple enough and scale is not a problem, the scaffold itself just gets stuff done?

I didnt know this thing about couch and redis though. That sounds sad.

I'm surprised by your statement about the difference between EM and node. I mean EM is definitely uglier than node, but otherwise I really don't see much of a problem. But maybe I've not done anything non-trivial in EM to really understand. But working on js annoys me. Partly because of the language issues, design choices and the poor pre-existing library. So while I can work with js, it's just not as convenient as working with ruby imo.  

But more than anything else, the fucking dynamic scoped this. I'm sick of reading code with this and self and seeing refactoring attempts result in subtle bugs. This more than anything else bugs me about working with js, especially on node. :/