This week, while wandering around GitHub, I stumbled onto an interesting project: ry/node. Don't let the plain name fool you; there is a real idea hiding underneath. A developer named Ryan Dahl started it at the beginning of the year, settled on the name "Node" in March, and quietly pushed the first releases out in late May. And today (yes, literally today) version 0.1.7 came out. A project that covers this much ground in three months could not be ignored. I've been poking at it for a week, so here we are.
What is this Node thing?
The project's own one-liner says it best: "Event-based I/O for V8 javascript." In plain terms: it takes V8, the fast JavaScript engine Google unveiled with Chrome last year, pulls it out of the browser and sits it on a server. Around the engine it wraps an event-driven layer for TCP, HTTP, DNS and file operations. The stated goal is ambitious too: "an easy way to build scalable network programs."
Yes, JavaScript. On the server. Not a joke.
What's the point?
In the classic web server model, every connection gets its own operating-system thread. Every thread arrives carrying its own memory stack, measured in megabytes. Dan Kegel's famous C10k problem is exactly about this: try greeting ten thousand concurrent clients with that model and things turn into torture.
Node does the opposite. One process, one event loop. It tells the operating system (through epoll, kqueue, /dev/poll or select, depending on the platform) "wake me when a new connection arrives or the timer expires" and goes to sleep. Each connection is just a small heap allocation, not a giant thread stack. There are no locks, so there is nothing to deadlock. The claim on Dahl's site reads exactly like this: because nothing ever blocks, even less-than-expert programmers can build fast systems. A bold sentence. But after a week of tinkering, I can report that the man is onto something.
A web server in six lines
Here is the example straight from the project's homepage; a web server that answers "Hello World" after waiting two seconds:
node.http.createServer(function (req, res) {
setTimeout(function () {
res.sendHeader(200, {"Content-Type": "text/plain"});
res.sendBody("Hello World");
res.finish();
}, 2000);
}).listen(8000);
puts("Server running at http://127.0.0.1:8000/");
That is the entire program. Yes, all of it.
% /usr/local/bin/node example.js
Server running at http://127.0.0.1:8000/
The real trick is invisible in the code: during that two-second wait, the server cheerfully keeps accepting and serving other requests. Try it with two browser windows side by side; each gets its answer at its own pace. In a threaded framework you would have spent the week reasoning about worker pools. Here it falls out of the model for free.
Installation follows the classic Unix ritual. V8 comes bundled in the tarball, so there are no other dependencies, but the build system wants Python 2.4 or newer:
./configure
make
make install
At the moment it is only tested on Linux, Macintosh and FreeBSD. (Extra info: Windows support is supposed to arrive someday via MinGW. Not today.)
How it differs from EventMachine and Twisted
The evented-server idea itself is not new; Ruby has EventMachine, Python has Twisted. Dahl's claim is that Node pushes it one step further. In those systems you always end your script with a blocking call that starts the event loop, something like EventMachine::run(). In Node there is no such call. Your script runs, registers its callbacks, and Node enters the event loop on its own, leaving when there is nothing left to do. In other words, the event loop is not a library you call; it is the environment itself. Just like JavaScript in the browser.
HTTP, by the way, is a first-class citizen here. The HTTP parser and API grew out of Dahl's own frustrations with existing web servers. Streaming a response through most web frameworks ranges from painful to impossible, and Node is deliberately built to make it natural. It looks like a very solid foundation for new web frameworks.
Where did the idea come from?
The story is a charming one. According to Dahl, the inspiration was a file-upload progress bar on Flickr. The browser had no idea how much of the file had arrived, so it kept asking the server. "How much left?" "And now?" If the server could simply report events as they happened, none of that dance would be necessary. Node is exactly what he built instead of complaining about it.
And this is not his first attempt. He previously wrote Ebb, an event-driven HTTP server in C aimed at the Ruby on Rails crowd. Same obsession, this time in JavaScript. (Note: obsessions sometimes pay off.)
Three months of motion
The pace of development is astonishing. The mid-June 0.0.x releases were still rough sketches. The end-of-June 0.1.0 brought the EventEmitter and Promise interfaces, and the whole API now sits on top of them. Earlier this month 0.1.2 added a DNS API. And today's 0.1.7 delivers a global process object, a new node.stdio, a promise.wait() helper and a synchronous require(). V8 has been bumped to 1.3.7 as well.
Fair warning, though: the API bends, breaks and mends a little with every release. Code you write against 0.1.7 today may ask for a touch-up in a month. There is no package manager either; you find libraries on GitHub and clone them. So this is still a toy for the curious, not for your boss's billing system.
See it in action
Dahl runs a chat room written in Node at chat.tinyclouds.org. He hasn't skipped the footnote either: "not stable, might occasionally be down." Honesty deserves applause. The full source sits on GitHub as node_chat. For a three-month-old project, the community is surprisingly alive: a mailing list on Google Groups and the #node.js channel on freenode. The door is open if you feel like sending patches.
So, will it catch on?
Server-side JavaScript is actually an old dream. Netscape tried it with LiveWire back in 1996, and Mozilla's Rhino has been running JavaScript on the JVM for years; neither quite set the world on fire. But this January, Kevin Dangoor started the ServerJS mailing list to standardize exactly this space (the group renamed itself CommonJS just this month), and now Node shows up out of nowhere. Something is in the air.
The name is also a hint about the future: a single Node process is deliberately single-threaded. Scaling to multi-core machines is meant to happen through message passing between processes; the site even floats the Web Workers API for spawning them. So a "node" is a node in a network of processes.
Will Node be running production systems a year from now? I don't know. But for the first time in a long while, writing a network server feels fun. /usr/local/bin/node stays in my PATH.
Let's see what comes of it.
