Node.js allows standalone Javascript programs to use evented I/O like EventMachine or Python's Twisted, Grand Central Dispatch's Dispatch Sources and queues and many other similar systems.
This presentation discusses implementation and design ideas behind Node.js (PDF Link). Essentially, Node.js allows to write code like this (from the linked slides):
http.createServer( function (req,res) { res.sendHeader(200, {"Content-Type": "text/plain"}); res.sendBody("Hello\r\n"); res.sendBody("World\r\n"); res.finish(); }).listen(8000);
The createServer
call sets up the system to listen to port 80; when a connection comes in, the anonymous function that is passed in is invoked. There's no need to manually fire up threads or dispatch incoming requests, Node.js takes care of that.
This is just one example, a lot of other I/O, both reading and writing, can also be handled with callbacks and events. Non-blocking reads from stdin
are possible as well.
The the event concept is used throughout Node.js' APIs. Many different systems allow to register listeners for all kinds of events, simply by using the addListener
method. For instance, the global process
object, allows to listen to OS signals with this code (from the presentation slides):
process.addListener("SIGINT", function () { puts("good bye"); process.exit(0) } );
Other libraries return Promise objects which emit one of these events: "success", "error", "cancel". An example from the Node.js API docs:
var posix = require("posix"), sys = require("sys"); var promise = posix.unlink("/tmp/hello"); promise.addCallback(function () { sys.puts("successfully deleted /tmp/hello"); });
The promise.addCallback
call registers a listener that's called when the Promise emits the "success" event - in this example, when the API call has finished successfully. Something similar can be achieved by wait
or timeout
calls on the Promise object, which block indefinitely or until the timeout is triggered.
With the Promise concepts, Node.js can execute blocking calls without actually blocking the Javascript code. These calls are performed on a background thread which, after completion, triggers the Promise events.
To achieve its design goals, Node.js takes Google's V8 and bundles it with a few libraries:
- libev implements the event loop and abstracts the underlying specific technologies use(such as (
select
,epoll
, etc) - libeio uses a thread pool to execute blocking calls in the background. udns helps in that regard by implementing a non-blocking DNS resolution library (DNS resolution is often only available as a blocking syscall in Operating Systems).
- protocol implementations like http-parser
There are already quite a few libraries built upon Node.js, bindings to databases, protocols like BERT-RPC and many others.
Support for the HTML5 Web Workers API is considered; Web Workers allow Javascript to start new workes that executes tasks. Unlike shared memory threads, Web Workers don't see each other's (or their creator's) memory; communication happens only via message passing, which makes them a bit like Erlang processes.
Node.js ships with an executable that runs Javascript files, as well as a shell (REPL) that makes it easy to try out Javascript code.
For more information, Simon Willison has an article covering Node.js; the article also provides some examples of Node.js libraries, eg. for accessing Redis or building web apps.
Node.js allows to use Javascript as more than a GUI scripting language. It does inherit one problem from Javascript, though: the lack of a standard Javascript library. Node.js comes with libraries, and the mentioned 3rd party libraries certainly help, but it's still not close to the functionality found in Java's or Ruby's standard libraries. With Javascript's increasing popularity for writing client applications, it's possible more general purpose, non-browser-based libraries will appear.
Would you consider using Node.js? What would you use it for?