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.

Alexey Taktarov

May 07, 2012
Tweet

More Decks by Alexey Taktarov

Other Decks in Programming

Transcript

  1. milk = dear_son.please_buy_a_milk();

    View full-size slide

  2. milk = dear_son.please_buy_a_milk();

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  5. milk = dear_son.please_buy_a_milk();

    View full-size slide

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

    View full-size slide

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

    View full-size slide

  8. 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

    View full-size slide

  9. In terms of:
    1. OS ( process, threads scheduling)
    2. Application ( event-loops,
    overlapped system calls, callbacks )

    View full-size slide

  10. OS can handle it.
    So why should I know about
    event-loops and other sh!t?!

    View full-size slide

  11. That’s why

    View full-size slide

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

    View full-size slide

  13. 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

    View full-size slide

  14. setTimeout(function() {
    console.log('Hello, ');
    }, 1000);
    console.log(' World!');
    Dummy example #1

    View full-size slide

  15. 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');

    View full-size slide

  16. Web-Server
    |
    CGI
    |
    php, perl, python, etc.

    View full-size slide

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

    View full-size slide

  18. Features
    1. Clustering
    2. Packages: web-frameworks, DB,
    graphics, BNF parsers, unit-
    testing, … + package manager
    3. Cloud 9 IDE (runs in browser)

    View full-size slide

  19. Problems
    Asynchronous logic hell!

    View full-size slide

  20. Problems
    Asynchronous logic hell!
    1. Using helper modules (deferred,
    async, node-fibers)
    2. Program is Finite State Machine

    View full-size slide

  21. The main problem
    Node.Js is very young, so
    be careful!
    Use it just-for-fun!

    View full-size slide

  22. 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

    View full-size slide

  23. Have a nice milk!
    Speaker: Taktarov A.
    Drawings: Taktarov E.
    molefrog[at]gmail.com

    View full-size slide