read

Updated 2011-04-27: forgot to add Backbone.js

Most of the net is abuzz about Node, so it’s hard to miss it. I’ve been sitting on the sidelines for several months now, but after attending a TechTalksTO talk on Node given by James Duncan of Joyent, I’m convinced I should dive in.

tl;dr version

  • Like C, Javascript is here to stay.
  • Asynchronous (everywhere) is the future.
  • Node is an exciting platform you have to check out.

What is Node?

Node is a server side Javascript implementation. As James put it, Node is the Google Chrome V8 Javascript engine and libev glued together. V8 provides the super fast Javascript virtual machine while libev contributes the event loop. Of course, it’s more complicated than that, but those are the basics.

To make full use of this fast underpinning, Node follows a non-blocking, fully asynchronous programming mode, and encourages libraries to follow its example.

Why Node and Not Another Server Side Javascript?

The best known server side Javascript implementation is Rhino. However, as Dhanji Prasanna asked on Twitter,

Are there any fast alternatives to Rhino on JVM?

Most of the server side Javascript implementations didn’t grasp the importance of non-blocking, asynchronous I/O.

Why Asynchronous?

Asynchronous programming is usually associated with GUI development where everything is event driven and asynchronous. Why would you bring this to the server? Well, for one, blocking sucks. James made this point very potent by showing the amount of CPU cycles spent accessing each layer in a computer. Starting from L1 cache, L2 cache, RAM, disk and finally the network, each layer adds many orders of magnitude to the amount of time the CPU sits blocked, waiting for the call to return.

That’s why you want non-blocking I/O. As the Mashable article (above) said about the history of Node, Ryan Dhal tried the Node approach on a number of languages, in all cases failing, because of the culture of synchronous, blocking programming. But Javascript, being a front end language, has always been event driven and asynchronous.

How Much Does This Buy You? (Easy C10K)

What does this approach really buy you? As James pointed out, it buys you easy C10K. What’s that you ask? 10 thousand concurrent connections. In fact, Node seems to be able to handle 100K concurrent connections on a single, mildly beefy server. In benchmarks, Node competes at the level of Nginx, a C based web framework built around the same principles. This is a testament to the power of V8.

Asynchronous programming has been around for a long time, but has started to gain popularity recently. The benefits are evident, take the recent search speed increase at Twitter after they adopted Netty and an asynchronous model.

Alright, I’m sold. Now what?

Getting started with Node is easy, if you’re on a Mac, I highly recommend installing it through Homebrew.

brew install node

Now try out Node’s obligatory “Hello, World!”. Create server.js,

var http = require('http');

http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type': 'text/plain'});
  res.end('Hello World\n');
}).listen(8080, "127.0.0.1");

console.log('Server running at http://127.0.0.1:8080/');

Then run

node server.js

And voilà! Your first node app.

Great… Anything Else?

Tons. One of Node’s biggest strengths is the huge community that’s sprouted around it. As RedMonk discussed in their recent Programming Language Framework Traction on Hacker News, “What’s interesting is that the much younger Node.js is beginning to approach Django both in growth rate and visibility.” This is evident by the number of new libraries and the speed at which they are being developed.

The best way to install plugins is to use npm. Some of the more popular plugins are,

  • express – a Sinatra inspired framework.
  • socket.io – server push support.
  • backbone.js – a popular MVC framework for Javascript, works with Node as well.
  • jsdom – web scraping 2.0, powered by Node.
  • node.io – another scraping library, this looks even better then jsdom.
  • Mustache – template engine, used by Twitter (the JS version).
  • Jade – another template engine, inspired by HAML.
  • stylus – a SCSS or LESS inspired CSS engine.
  • mongoose – MongoDB ORM layer.

A Look at Express

The Node ecosystem is growing by leaps and bounds, but I wanted to highlight one of the more popular libraries. Making web application development in Node easier, Express has become a trademark in most Node web apps. Let’s see how easy it is to use.

var app = express.createServer();

app.get('/', function(req, res){
    res.send('Hello World');
});

app.listen(8080);

Hosting

You’re all excited to get started on Node, but you’re wondering where to host your new Node application. There is a great stackoverflow question that has a lot of options. Another recent contender in hosting Node applications is VMware with Cloud Foundry. I’ve tried both Joyent’s http://no.de and Cloud Foundry.

Node is an exciting platform that’s experiencing explosive growth. If you’ve been sitting on the fence, now is a great time to jump in.

More Resources

Blog Logo

Arthur Maltson


Published

Image

Technical Dev Blog

Technical development blog of Arthur Maltson. Covers many topics from Java to Ruby to DevOps.

Back to Overview