Node.js

2,390 views
2,335 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,390
On SlideShare
0
From Embeds
0
Number of Embeds
4
Actions
Shares
0
Downloads
44
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Node.js

  1. 2. @ samshull Who is this guy? Follow along: http://ta.gd/samsnodepresentation
  2. 3. Topics <ul><li>What is it? How does it differ? </li></ul><ul><li>Why to use it? </li></ul>
  3. 4. What is node.js? <ul><li>Evented I/O for JavaScript </li></ul>Server Side JavaScript Runs on Google's V8 JavaScript Engine
  4. 5. <ul><li>node.js is not threaded! </li></ul>Node.js uses a event driven non-blocking concurrency model Like a web browser!
  5. 6. Synchronous Model
  6. 7. Threaded Model
  7. 8. Evented Model
  8. 9. How does it differ? <ul><li>&quot;Node is similar in design to and influenced by systems like Ruby's Event Machine or Python's Twisted . Node takes the event model a bit further—it presents the event loop as a language construct instead of as a library.&quot; </li></ul><ul><li>-- nodejs.org </li></ul>
  9. 10. Traditional I/O <ul><li>    var fs = require( 'fs' ), </li></ul><ul><li>          fd = fs.open( '/path/to/file' , 'r' ), </li></ul><ul><li>          line = fs.read(fd, 100 ); </li></ul><ul><li>    while (line) { </li></ul><ul><li>        console.log(line); </li></ul><ul><li>         line = fs.read(fd, 100 ); </li></ul><ul><li>    } </li></ul>
  10. 11. File Read from Stream <ul><li>     require( 'fs' ).createReadStream( '/path/to/file' , { </li></ul><ul><li>                  </li></ul><ul><li>                  'flags' : 'r'              </li></ul><ul><li>              , 'bufferSize' : 4 * 1024   </li></ul><ul><li>     }).on( 'data' , function (data) { </li></ul><ul><li>         console.log(data);     }); </li></ul>
  11. 12. Basic HTTP Server <ul><li>     require( 'http' ).createServer( function (req, res) {    </li></ul><ul><li>  </li></ul><ul><li>         res.writeHead( 200 , { 'Content-Type' : 'text/plain' });     </li></ul><ul><li>         res.end( 'Hello World ' ); </li></ul><ul><li>      </li></ul><ul><li>     }).listen( 8124 , '127.0.0.1' ); </li></ul>
  12. 13. Events <ul><li>A TCP server emits a `connection` event each time a connection is made. </li></ul><ul><li>A HTTP request emits a `data` event each time a packet is received. </li></ul><ul><li>A readable file stream emits a `data` event each time the requested buffer is read into memory. </li></ul>
  13. 14. Typical Web Page Client Server Database
  14. 15. Web Application Client Server Database
  15. 16. Future Web Application Client Server Database
  16. 17. Why to use it? <ul><li>&quot;Node's goal is to provide an easy way to build scalable network programs.&quot;  </li></ul><ul><li>--nodejs.org </li></ul>&quot;Node perfectly fills the proxy and authentication layer between the storage backend and client&quot;  --nodejs.org No waiting for a blocking database call or file system call allows developers to create faster systems
  17. 18. Benefits Efficient resource multiplexing by preventing MUTEX locks Multi-process resource sharing with libraries, like multi-node, take advantage of multi-core CPUs Up to 20,000 streams can be handled by a single thread without thread safety issues
  18. 19. Awesomeness <ul><ul><li>It's fast </li></ul></ul><ul><ul><li>It can handle tons of concurrent requests </li></ul></ul><ul><ul><li>It's written in JavaScript (which means you can use the same code server side and client side...way cool) </li></ul></ul>* http://www.synchrosinteractive.com/blog/3-websites/22-nodejs-has-a-bright-future Platform Number of Requests per seconds PHP (via Apache) 3187.27 Static (via Apache) 2966.51 Node.js 5569.30
  19. 20. Links <ul><li>     http://github.com/ry/node - Source code </li></ul><ul><li>  </li></ul><ul><li>     http://nodejs.org/ - Documentation </li></ul><ul><li>     http://ta.gd/realtimemousetracking - Real Time Example </li></ul><ul><li>  </li></ul><ul><li>    http://github.com/kriszyp/multi-node - Multi-node </li></ul>

×