Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Got Milk? A short introduction to node.js and event-driven programming.

Got Milk? A short introduction to node.js and event-driven programming.

A short introduction to node.js and event-driven programming.

01bf97df3784afa10fc2b561e251a41c?s=128

Alexey Taktarov

May 07, 2012
Tweet

Transcript

  1. Got milk?

  2. None
  3. milk = dear_son.please_buy_a_milk();

  4. milk = dear_son.please_buy_a_milk();

  5. milk = dear_son.please_buy_a_milk(); dear_son.thank_you(); milk.drink();

  6. milk = dear_son.please_buy_a_milk(); dear_son.thank_you(); milk.drink(); Blocking operations

  7. milk = dear_son.please_buy_a_milk();

  8. milk = dear_son.please_buy_a_milk(); do_cleanup(); do_something_else();

  9. milk = dear_son.please_buy_a_milk(); do_cleanup(); do_something_else(); /* ? dear_son.here_is_your_milk(); */ Non-Blocking

    operations
  10. Event-Loop

  11. 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
  12. In terms of: 1. OS ( process, threads scheduling) 2.

    Application ( event-loops, overlapped system calls, callbacks )
  13. OS can handle it. So why should I know about

    event-loops and other sh!t?!
  14. That’s why

  15. Introducing Node.Js! Event-driven I/O server-side JavaScript framework based on V8

  16. Why Javascript? 1. It’s awesome 2. It’s simple 3. It’s

    popular 4. V8 is really fast 5. Built-in support for callbacks, lambdas, closures and other magic
  17. setTimeout(function() { console.log('Hello, '); }, 1000); console.log(' World!'); Dummy example

    #1
  18. Dummy example #2 var 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');
  19. Web-Server | CGI | php, perl, python, etc.

  20. Node.Js (all-in-one: web-server, caching, parsing, templating, data-storing, doing-a-sandwich)

  21. Features 1. Clustering 2. Packages: web-frameworks, DB, graphics, BNF parsers,

    unit- testing, … + package manager 3. Cloud 9 IDE (runs in browser)
  22. Problems Asynchronous logic hell!

  23. Problems Asynchronous logic hell! 1. Using helper modules (deferred, async,

    node-fibers) 2. Program is Finite State Machine
  24. The main problem Node.Js is very young, so be careful!

    Use it just-for-fun!
  25. Resources 1. “UNIX internals” by U.Vahalia 2. “Thinking Asynchronous in

    C++” by author of boost::asio http://blog.think-async.com/ 3. Online npm repository http://search.npm.js 4. Cloud 9 IDE http://c9.io
  26. Have a nice milk! Speaker: Taktarov A. Drawings: Taktarov E.

    molefrog[at]gmail.com