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. Got milk?

    View Slide

  2. View Slide

  3. milk = dear_son.please_buy_a_milk();

    View Slide

  4. milk = dear_son.please_buy_a_milk();

    View Slide

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

    View Slide

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

    View Slide

  7. milk = dear_son.please_buy_a_milk();

    View Slide

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

    View Slide

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

    View Slide

  10. Event-Loop

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

  14. That’s why

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  22. Problems
    Asynchronous logic hell!

    View Slide

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

    View Slide

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

    View Slide

  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

    View Slide

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

    View Slide