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