A short introduction to node.js and event-driven programming.
Got milk?
View Slide
milk = dear_son.please_buy_a_milk();
milk = dear_son.please_buy_a_milk();dear_son.thank_you();milk.drink();
milk = dear_son.please_buy_a_milk();dear_son.thank_you();milk.drink();Blocking operations
milk = dear_son.please_buy_a_milk();do_cleanup();do_something_else();
milk = dear_son.please_buy_a_milk();do_cleanup();do_something_else();/* ? dear_son.here_is_your_milk(); */Non-Blocking operations
Event-Loop
milk = dear_son.please_buy_a_milk();/* Event loop starts - while(…) */if( dear_son.milk_ready() ) break;do_some_cleanup();/* Event loop ends */Event-Loop
In terms of:1. OS ( process, threads scheduling)2. Application ( event-loops,overlapped system calls, callbacks )
OS can handle it.So why should I know aboutevent-loops and other sh!t?!
That’s why
Introducing Node.Js!Event-driven I/Oserver-side JavaScript frameworkbased on V8
Why Javascript?1. It’s awesome2. It’s simple3. It’s popular4. V8 is really fast5. Built-in support for callbacks,lambdas, closures and othermagic
setTimeout(function() {console.log('Hello, ');}, 1000);console.log(' World!');Dummy example #1
Dummy example #2var http = require(‘http’);http.createServer(function (req, res) {res.writeHead(200,{'Content-Type': ‘text/plain'});res.end('Hello World\n');}).listen(1337, '127.0.0.1');console.log('Server running at port 1337');
Web-Server|CGI|php, perl, python, etc.
Node.Js(all-in-one:web-server, caching, parsing,templating, data-storing,doing-a-sandwich)
Features1. Clustering2. Packages: web-frameworks, DB,graphics, BNF parsers, unit-testing, … + package manager3. Cloud 9 IDE (runs in browser)
ProblemsAsynchronous logic hell!
ProblemsAsynchronous logic hell!1. Using helper modules (deferred,async, node-fibers)2. Program is Finite State Machine
The main problemNode.Js is very young, sobe careful!Use it just-for-fun!
Resources1. “UNIX internals” by U.Vahalia2. “Thinking Asynchronous in C++”by author of boost::asiohttp://blog.think-async.com/3. Online npm repositoryhttp://search.npm.js4. Cloud 9 IDEhttp://c9.io
Have a nice milk!Speaker: Taktarov A.Drawings: Taktarov E.molefrog[at]gmail.com