1. Motivation
  2. Benchmarks
  3. Download
  4. Build
  5. API
    1. Timers
    2. File System
    3. TCP
    4. HTTP
    5. Modules

Node

Node is a purely evented I/O framework for V8 javascript. It enables easy development of highly concurrent, efficent network programs. For example, this is a simple web server which responds with "Hello World" after waiting two seconds:

node.http.server(function (msg) {
  setTimeout(function () {
    msg.sendHeader(200, [["Content-Type", "text/plain"]]);
    msg.sendBody("Hello World");
    msg.finish();
  }, 2000);
}).listen(8000, "localhost");

While one request is waiting the server will continue to accept and serve other requests. This little server can handle hundreds of concurrent requests while using little CPU or memory—see benchmarks. The example demonstrates efficency that is needed for handling long held "comet" requests.

Node is free to download, use, and build upon.

Motivation

  1. Evented programming makes sense
    1. difference between blocking/non-blocking design
    2. latency figures
    3. purely evented interfaces rule out a lot of stupidity
  2. Evented programs are more efficient
    1. pthread stack size 2mb default stack size on linux (1mb on windows, 64kb on FreeBSD) of course this is adjustable
    2. context switching benchmark
    3. Apache vs. Nginx
    4. event machine vs mongrel (neverblock)
  3. The appropriateness of Javascript
    1. No I/O
    2. No Threads
    3. Good compiler
    4. Universality of the language
Threads are a bad model.

I/O latency

    l1 cache ~ 3
    l2 cache ~ 14
         ram ~ 250
        disk ~ 41000000
     network ~ 240000000
    

Javascript is without I/O. In the browser the DOM provides I/O, but non-browser javascript interpreters have only non-standardized functions to allow them print to console or access the network.

Javascript is without the concept of threads.

Contemporary computer infrastructure has two irreplaceable languages: C and Javascript. C is the language of operating systems. POSIX, the universal operating system API, is defined in C. So while you can interface with operating systems in Java and Haskell, those languages access must make system calls in C. Similarly, Javascript is the language of the web operating system. In place of POSIX is the DOM. You can wrap Javascript, you can compile to Javascript, but in the end browsers must be interfaced with in Javascript. Portable low-level systems tend to be written in C and portable web-level systems are written in Javascript.

Evented vs Threaded Servers

There are many methods to write internet servers but they can fundamentally be divided into two camps: evented and threaded; non-blocking and blocking. A blocking server accepts a connection and launches a new thread to handle the connection. Because the concurrency is handled by the thread scheduler, a blocking server can make function calls which preform full network requests.

var response = db.execute("SELECT * FROM table");
// do something

An evented server manages its concurrency itself. All connections are handled in a single thread and callbacks are executed on certain events: "socket 23 is has data to read", "socket 65's write buffer is empty". An evented server executes small bits of code but never blocks the process. In the evented world callbacks are used instead of functions

db.execute("SELECT * FROM table", function (response) {
  // do something
});

Benchmarks

Download

Build

configure
make
make install

Application Programming Interface

The node executable should be given an argument pointing to a javascript file.

Timers

The timer API is the same as in the browser. The functions setTimeout, setInterval, cancleTimeout, and cancleInterval

File System

TCP

HTTP

Modules