Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Node.js
A Guided Tour


Presenter: C. Aaron Cois, Ph.D.
The web is changing
 It   used to be about consumption
Real-time Interaction
Real-time Interaction
 Real-Time   Games
 Chat
 Stock tickers
 Twitter Feeds
 Collaboration
 Creation


…on a massiv...
What do we need to make it
happen?
 Fast,   persistent I/O
     HTTP wasn’t built for this
     Server’s need to push d...
Outline
 What is Node.js?
 Technology Overview
 How does it work?
 Demo
 Code!
 Deployment and Hosting
What is Node.js?
Node.js
 Node.js
        is an event-driven, server-side
 JavaScript environment
    Based on the V8 JavaScript Engine,
...
Platforms
 Runs    on OSX, Linux, Windows

 Clickable   installers for:
     Windows
     Mac OSX


 Linux   has apt-...
Why Node?
 Node.js is specifically designed for
  building fast, efficient, scalable network
  applications
 Node uses a...
Technology: V8 Engine
   Developed by Google
   Ships with the Google Chrome web browser
   Allows Chrome to run JavaSc...
Technology: JavaScript
 JavaScript      is:
    A fully-functional programming language
      Capable of doing anything...
Technology: JavaScript2
 There’sno reason the JavaScript
 language can’t be used elsewhere (say,
 server-side)
     This...
Who is using Node.js?
How does it work?
The Basic Idea


    I/O is expensive
*http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
Ways to deal with I/O
 Synchronous
     One requests at a time, first come, first serve


 Fork
     New process for e...
Another Thesis


Thread-per-connection
 is memory-expensive
Traditional Threaded Model
N  worker threads/processes
 Each incoming connection handed
  to a worker
  That   worker i...
The life of a worker…




     Waiting on   Waiting on   Waiting on DB…
     File I/O…    Network
                  Respon...
The life of a worker…

                  Blocking Wastes Cycles




     Waiting on        Waiting on          Waiting on ...
The Other Basic Idea


Writing (Good) Threaded
    Code is DIFFICULT
The life of N workers…


Thread 1



Thread 2



Thread 3



Thread 4


                         Time
The life of N workers…


Thread 1



Thread 2



Thread 3



Thread 4


                         Time
The life of N workers…


Thread 1



Thread 2

            ALL PROCESSES IDLE
Thread 3



Thread 4


                     ...
Even worse…
 If
   all threads are in use, every incoming
  connection is blocked


 Thiscan cause
  massive traffic jam...
Is this the only way?



                         There is
                        another…
The Node.js way
   Axiom:
       Multi-Threaded code
         Is difficult to write
         Is difficult to debug
   ...
Single threaded?!?
     Skeptical? I don’t blame you




          But hear me out…
Node.js Event Loop
 Event   Loop (from Wikipedia):
    A “construct that waits for and dispatches
     events or message...
Node.js Event Loop




         DB I/O command to event loop

       Net I/O command to event loop
     File I/O command t...
Node.js Event Loop




         DB I/O command to event loop

       Net I/O command to event loop
     File I/O command t...
Node.js app code…
 Isrun entirely in a single thread
 Passes I/O requests to the event loop,
  along with callbacks

 Y...
Callback example


var filename = “test_file.txt”;

fs.open(filename, “w”, function(err, file) {
    if (err) throw err;
}...
Callback example
   Filesystem module forwards task to event loop


var file = (“test_file.txt”);

fs.open(file, “w”, func...
Callback example
                          Callback is invoked when work is complete




var file = (“test_file.txt”);

fs...
This is not magic
   The    following:


for(i=0; i<5; i++) {
   sleep(1000);
}


   Will
       block the entire Node e...
Node is in charge
 Let   Node.js handle
     Dispatch
     Concurrency
     (most) Async operations

 What    Node do...
Interlude: Modules
Node.js API
   Node provides an API in the form of modules
    (a.k.a. libraries)
       Modules work with event loop to...
A Few Good Modules
   Net           Network socket support

   HTTP          HTTP communication

   File System   File ...
Node.js Community
 Additionally,
              the Node.js community
 maintains excellent modules to enhance
 the capabil...
Notable Community Modules
 Express              Web App Framework

 Socket.io            Websockets, etc

 Passport/Eve...
Let’s see some code!
Node Web Server

var http = require('http');


http.createServer(function (req, res) {
  res.writeHead(200, {'Content-Type...
Node TCP Listener
var net = require('net');

var server = net.createServer(function (socket) {
    socket.write('Echo serv...
Live Demo


http://techfestchat.jit.su


      Hosting courtesy of Nodejitsu
Any .NET Devs in the room?

 Microsoft has been expending a lot of
 effort to make Node a first class
 framework



And n...
A sneak preview!
MS WebMatrix 2
   Open WebMatrix 2
Node.js Deployment and
Hosting
Cloud Hosting
   Heroku
   Microsoft Azure
   Nodejitsu
   Cloud Foundry
   Nodester
   DotCloud
   Appfog
   Joye...
Thanks!
   Feel free to look me up at:

http://www.codehenge.net

I love questions, collaborations, and talking with
peop...
Node.js: A Guided Tour
Node.js: A Guided Tour
Node.js: A Guided Tour
Upcoming SlideShare
Loading in …5
×

of

Node.js: A Guided Tour Slide 1 Node.js: A Guided Tour Slide 2 Node.js: A Guided Tour Slide 3 Node.js: A Guided Tour Slide 4 Node.js: A Guided Tour Slide 5 Node.js: A Guided Tour Slide 6 Node.js: A Guided Tour Slide 7 Node.js: A Guided Tour Slide 8 Node.js: A Guided Tour Slide 9 Node.js: A Guided Tour Slide 10 Node.js: A Guided Tour Slide 11 Node.js: A Guided Tour Slide 12 Node.js: A Guided Tour Slide 13 Node.js: A Guided Tour Slide 14 Node.js: A Guided Tour Slide 15 Node.js: A Guided Tour Slide 16 Node.js: A Guided Tour Slide 17 Node.js: A Guided Tour Slide 18 Node.js: A Guided Tour Slide 19 Node.js: A Guided Tour Slide 20 Node.js: A Guided Tour Slide 21 Node.js: A Guided Tour Slide 22 Node.js: A Guided Tour Slide 23 Node.js: A Guided Tour Slide 24 Node.js: A Guided Tour Slide 25 Node.js: A Guided Tour Slide 26 Node.js: A Guided Tour Slide 27 Node.js: A Guided Tour Slide 28 Node.js: A Guided Tour Slide 29 Node.js: A Guided Tour Slide 30 Node.js: A Guided Tour Slide 31 Node.js: A Guided Tour Slide 32 Node.js: A Guided Tour Slide 33 Node.js: A Guided Tour Slide 34 Node.js: A Guided Tour Slide 35 Node.js: A Guided Tour Slide 36 Node.js: A Guided Tour Slide 37 Node.js: A Guided Tour Slide 38 Node.js: A Guided Tour Slide 39 Node.js: A Guided Tour Slide 40 Node.js: A Guided Tour Slide 41 Node.js: A Guided Tour Slide 42 Node.js: A Guided Tour Slide 43 Node.js: A Guided Tour Slide 44 Node.js: A Guided Tour Slide 45 Node.js: A Guided Tour Slide 46 Node.js: A Guided Tour Slide 47 Node.js: A Guided Tour Slide 48 Node.js: A Guided Tour Slide 49 Node.js: A Guided Tour Slide 50 Node.js: A Guided Tour Slide 51 Node.js: A Guided Tour Slide 52 Node.js: A Guided Tour Slide 53 Node.js: A Guided Tour Slide 54 Node.js: A Guided Tour Slide 55 Node.js: A Guided Tour Slide 56 Node.js: A Guided Tour Slide 57
Upcoming SlideShare
Understanding the Single Thread Event Loop
Next
Download to read offline and view in fullscreen.

2 Likes

Share

Download to read offline

Node.js: A Guided Tour

Download to read offline

These are the slides to a talk I gave at Pittsburgh techFest 2012. The topic was an overview of the Node.js framework, and how you can use it to build amazing things.

* See more of my work at http://www.codehenge.net

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Node.js: A Guided Tour

  1. 1. Node.js A Guided Tour Presenter: C. Aaron Cois, Ph.D.
  2. 2. The web is changing  It used to be about consumption
  3. 3. Real-time Interaction
  4. 4. Real-time Interaction  Real-Time Games  Chat  Stock tickers  Twitter Feeds  Collaboration  Creation …on a massive scale
  5. 5. What do we need to make it happen?  Fast, persistent I/O  HTTP wasn’t built for this  Server’s need to push data to clients  Polling is slow and inefficient  Scalability  Usability  Maintainability
  6. 6. Outline  What is Node.js?  Technology Overview  How does it work?  Demo  Code!  Deployment and Hosting
  7. 7. What is Node.js?
  8. 8. Node.js  Node.js is an event-driven, server-side JavaScript environment  Based on the V8 JavaScript Engine, developed by Google  Most importantly, node.js is a  server-side runtime environment, that  compiles and executes JavaScript very efficiently.
  9. 9. Platforms  Runs on OSX, Linux, Windows  Clickable installers for:  Windows  Mac OSX  Linux has apt-get and yum
  10. 10. Why Node?  Node.js is specifically designed for building fast, efficient, scalable network applications  Node uses an event-driven, non-blocking I/O model to maximize efficiency
  11. 11. Technology: V8 Engine  Developed by Google  Ships with the Google Chrome web browser  Allows Chrome to run JavaScript code much faster  It does this by compiling the JavaScript directly into native machine code  As opposed to interpreting JavaScript, or execute it as bytecode  What does this mean for us, and for Node.js?
  12. 12. Technology: JavaScript  JavaScript is:  A fully-functional programming language  Capable of doing anything other traditional languages (C++, Java, Ruby, etc) can do  Has an excellent event model  Traditionally resigned to the context of the web application frontend  i.e. running inside a web browser
  13. 13. Technology: JavaScript2  There’sno reason the JavaScript language can’t be used elsewhere (say, server-side)  This is where node.js comes in, executing JavaScript efficiently on the server-side  JavaScript brings to Node.js:  Natural event-based programming, ideal for client-server applications  A known language, with low overhead
  14. 14. Who is using Node.js?
  15. 15. How does it work?
  16. 16. The Basic Idea I/O is expensive
  17. 17. *http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
  18. 18. Ways to deal with I/O  Synchronous  One requests at a time, first come, first serve  Fork  New process for each request  Threads  New thread for each request *http://www.nightmare.com/medusa/async_sockets.html
  19. 19. Another Thesis Thread-per-connection is memory-expensive
  20. 20. Traditional Threaded Model N worker threads/processes  Each incoming connection handed to a worker  That worker is now “in use”, and can handle no other connection, even if it is waiting on:  FileI/O  DB I/O  Network I/O  etc
  21. 21. The life of a worker… Waiting on Waiting on Waiting on DB… File I/O… Network Response… Time
  22. 22. The life of a worker… Blocking Wastes Cycles Waiting on Waiting on Waiting on DB… File I/O… Network Response… Time
  23. 23. The Other Basic Idea Writing (Good) Threaded Code is DIFFICULT
  24. 24. The life of N workers… Thread 1 Thread 2 Thread 3 Thread 4 Time
  25. 25. The life of N workers… Thread 1 Thread 2 Thread 3 Thread 4 Time
  26. 26. The life of N workers… Thread 1 Thread 2 ALL PROCESSES IDLE Thread 3 Thread 4 Time
  27. 27. Even worse…  If all threads are in use, every incoming connection is blocked  Thiscan cause massive traffic jams on high-throughput applications
  28. 28. Is this the only way? There is another…
  29. 29. The Node.js way  Axiom:  Multi-Threaded code  Is difficult to write  Is difficult to debug  Sucks up more dev/test/maintenance cycles  Most often has inefficient performance  Conclusion:  Screw it: Write code using a single thread
  30. 30. Single threaded?!? Skeptical? I don’t blame you But hear me out…
  31. 31. Node.js Event Loop  Event Loop (from Wikipedia):  A “construct that waits for and dispatches events or messages in a program”  Instead of performing I/O ourselves, we dispatch I/O events to Node’s event loop  It handles threads, process optimization, concurrency, etc
  32. 32. Node.js Event Loop DB I/O command to event loop Net I/O command to event loop File I/O command to event loop Time
  33. 33. Node.js Event Loop DB I/O command to event loop Net I/O command to event loop File I/O command to event loop Open for more work! Time
  34. 34. Node.js app code…  Isrun entirely in a single thread  Passes I/O requests to the event loop, along with callbacks  Your code then:  Goes to sleep  Uses no system resources  Will be notified via callback when I/O is complete
  35. 35. Callback example var filename = “test_file.txt”; fs.open(filename, “w”, function(err, file) { if (err) throw err; });
  36. 36. Callback example Filesystem module forwards task to event loop var file = (“test_file.txt”); fs.open(file, “w”, function(err, file) { if (err) throw err; });
  37. 37. Callback example Callback is invoked when work is complete var file = (“test_file.txt”); fs.open(file, “w”, function(err, file) { if (err) throw err; });
  38. 38. This is not magic  The following: for(i=0; i<5; i++) { sleep(1000); }  Will block the entire Node event loop for 5 seconds
  39. 39. Node is in charge  Let Node.js handle  Dispatch  Concurrency  (most) Async operations  What Node doesn’t promise:  To not block when you tell it to  Order of execution (e.g. forked parallel processes)
  40. 40. Interlude: Modules
  41. 41. Node.js API  Node provides an API in the form of modules (a.k.a. libraries)  Modules work with event loop to dispatch async tasks  API modules are installed along with Node  They provide standard application framework functionality  STDIO: console logging, timing, tracing  File System: File system access  …etc
  42. 42. A Few Good Modules  Net Network socket support  HTTP HTTP communication  File System File system access  Crypto Cryptography  Streams STDIO  Many more…
  43. 43. Node.js Community  Additionally, the Node.js community maintains excellent modules to enhance the capabilities of Node  These modules operate as  Libraries  Toolkits  Frameworks  …and much more.
  44. 44. Notable Community Modules  Express Web App Framework  Socket.io Websockets, etc  Passport/Everyauth Authentication  Jade HTML Template Engine  Connect Middleware collection  Less Simplified CSS
  45. 45. Let’s see some code!
  46. 46. Node Web Server var http = require('http'); http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello Worldn'); }).listen(1337, '127.0.0.1');
  47. 47. Node TCP Listener var net = require('net'); var server = net.createServer(function (socket) { socket.write('Echo serverrn'); socket.pipe(socket); }); server.listen(1337, '127.0.0.1');
  48. 48. Live Demo http://techfestchat.jit.su Hosting courtesy of Nodejitsu
  49. 49. Any .NET Devs in the room?  Microsoft has been expending a lot of effort to make Node a first class framework And now…
  50. 50. A sneak preview!
  51. 51. MS WebMatrix 2 Open WebMatrix 2
  52. 52. Node.js Deployment and Hosting
  53. 53. Cloud Hosting  Heroku  Microsoft Azure  Nodejitsu  Cloud Foundry  Nodester  DotCloud  Appfog  Joyent (coming soon)  …
  54. 54. Thanks!  Feel free to look me up at: http://www.codehenge.net I love questions, collaborations, and talking with people!  If you are interested in formal Node.js learning, I also have a course available at: http://www.udemy.com/learn-nodejs-by- example/
  • BaluLoganathan

    Jul. 22, 2013
  • ssuser48d121

    May. 29, 2013

These are the slides to a talk I gave at Pittsburgh techFest 2012. The topic was an overview of the Node.js framework, and how you can use it to build amazing things. * See more of my work at http://www.codehenge.net

Views

Total views

3,554

On Slideshare

0

From embeds

0

Number of embeds

20

Actions

Downloads

64

Shares

0

Comments

0

Likes

2

×