8 - '[2] node.js in brief: Server-side Javascript Built on Google’s V8 Evented, non-blocking I/O. Similar to EventMachine or Twisted. CommonJS module system. 8000 lines of C/C++, 2000 lines of Javascript, 14 contributors.'
12 - '[3] I/O needs to be done differently.'
16 - '[4] Many web applications have code like this:'
20 - '[5] In many cases, just waiting for the response.'
24 - '[6] I/O latency L1: 3 cycles L2: 14 cycles RAM: 250 cycles DISK: 41,000,000 cycles NETWORK: 240,000,000 cycles'
28 - '[7] Better software can multitask. Other threads of execution can run while waiting.'
32 - '[8] Is that the best that can be done? A look at Apache and NGINX.'
36 - '[9] Apache vs NGINX'
40 - '[10] Apache vs NGINX'
44 - '[11] Apache vs NGINX'
48 - '[12] Context switching is not free Execution stacks take up memory For massive concurrency, cannot use an OS thread for each connection.'
52 - '[13] Green threads or coroutines can improve the situation dramatically BUT there is still machinery involved to create the illusion of holding execution on I/O.'
56 - '[14] Threaded concurrency is a leaky abstraction.'
60 - '[15] Code like this'
64 - '[16] But a line of code like this'
68 - '[17] db.query("select..", function (result) { // use result });'
72 - '[18] So why isn’t everyone using event loops, callbacks, and non-blocking I/O? For reasons both cultural and infrastructural.'
76 - '[19] Cultural Bias'
80 - '[20] Cultural Bias'
84 - '[21] Missing Infrastructure'
88 - '[22] Missing Infrastructure'
92 - '[23] Too Much Infrastructure'
96 - '[24] Too Much Infrastructure'
100 - '[25] Javascript designed specifically to be used with an event loop: Anonymous functions, closures. Only one callback at a time. I/O through DOM event callbacks.'
104 - '[26] The culture of Javascript is already geared towards evented programming.'
108 - '[27] This is the node.js project: To provide a purely evented, non-blocking infrastructure to script highly concurrent programs.'
112 - '[28] Design Goals'
116 - '[29] Design Goals'
120 - '[30] Design Goals'
124 - '[31] Design Goals'
128 - '[32] Design Goals'
132 - '[33] Usage and Examples'
136 - '[34] Download, configure, compile, and make install it.'
148 - '[37] % node hello_world.js hello'
152 - '[38] Change the “hello world” program to loop forever, but print an exit message when the user kills it. We will use the special object process and the "SIGINT" signal.'
156 - '[39] 1 2 3 4 5 6 7 8 9 10 11'
160 - '[40] process.addListener("SIGINT", ...);'
168 - '[42] Like process, many other objects in Node emit events.'
172 - '[43] A TCP server emits a "connection" event each time someone connects. An HTTP upload emits a "body" event on each packet.'
176 - '[44] All objects which emit events are are instances of process.EventEmitter.'
180 - '[45] Write a program which: starts a TCP server on port 8000 send the peer a message close the connection'
184 - '[46] 1 2 3 4 5 6 7 8 9 10'
188 - '[47] % node server.js & [1] 9120 % telnet localhost 8000 Trying 127.0.0.1... Connected to localhost. Escape character is ’ˆ]’. hello! Connection closed by foreign host. %'
192 - '[48] The "connection" listener can be provided as the first argument to tcp.createServer(), so the program can be simplified:'
200 - '[50] File I/O is non-blocking too. (Something typically hard to do.)'
204 - '[51] As an example, a program that outputs the last time /etc/passwd was modified:'
208 - '[52] A promise is a kind of EventEmitter which emits either "success" or "error". (But not both.) All file operations return a promise.'
212 - '[53] promise.addCallback(cb)'
216 - '[54] Simple HTTP Server:'
220 - '[55] % node http_server.js & [4] 27355 % curl -i http://localhost:8000/ HTTP/1.1 200 OK Content-Type: text/plain Connection: keep-alive Transfer-Encoding: chunked Hello World %'
224 - '[56] Streaming HTTP Server:'
228 - '[57] % node http_server2.js & [4] 27355 % curl http://localhost:8000/ Hello'
236 - '[59] But Node never forces buffering'
240 - '[60] 1 2 3 4 5 6 7 8 9 10 11 12 13'
244 - '[61] Demo / Experiment'
248 - '[62] Internal Design'
252 - '[63] V8 (Google) libev event loop library (Marc Lehmann) libeio thread pool library (Marc Lehmann) http-parser a ragel HTTP parser (Me) evcom stream socket library on top of libev (Me) udns non-blocking DNS resolver (Michael Tokarev)'
256 - '[64] Blocking (or possibly blocking) system calls are executed in the thread pool. Signal handlers and thread pool callbacks are marshaled back into the main thread via a pipe.'
260 - '[65] % node myscript.js < hugefile.txt'
264 - '[66] Solution: Start a pipe, and a “pumping thread”. Pump data from blocking fd into pipe. Main thread can poll for data on the pipe.'
276 - '[69] Questions...?'