KEMBAR78
node.js - Fast event based web application development | ODP
node.js Fast event based web application development Wien, 7. April 2011 Gerhard Hipfinger
What is node.js? A JavaScript runtime Based on Googles V8 JavaScript engine High performance async event handling Module system, I/O system bindings, supports all common network protocols
Availability Available on all major platforms and on Windows too (via Cygwin or MinGW)
Module Management You want to use module management as there are many available! Over 1100 NPM is your friend curl http://npmjs.org/install.sh | sh npm install express
Obligatory  Hello, World! console.log('Hello, node.js World!'); > node hello.js Hello, node.js World! Create file hello.js with the content: Than type:
Design Goals Only a small core library Extensible module system Non blocking I/O As close to system calls as possible
Non Blocking I/O First we look at traditional blocking I/O I/O is serialized and the execution time is sum(opA, opB) var a = getSomethingFromDatabase(); c onsole.log(a); v ar b =  getSomethingFromDatabase(); console.log(b);
Non Blocking I/O getSomethingFromDatabase(paramA, function(result) { console.log(result); }); getSomethingFromDatabase(paramB function(result) { console.log(result); }); Both queries are handled in an event queue and don't block each other Execution time is MAX(op1,op2)
Non Blocking I/O node.js uses epoll, kqueue or whatever is available at the underlying operating system So I/O does not waste your CPU cycles No busy waiting! Thread pool is used for everything else
(No) Threads The rest of your app is single threaded Easy programming model but is not useful for all kinds of apps
Your Webserver in 6 LOC var  http = require('http'); http.createServer( function  (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8888, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8888/'); Pretty simple, isn't it?
Ever needed to poll for filechange? var fs = require('fs'); fs.watchFile(__filename, function() { console.log('I was changed!'); process.exit(0); });
So what can I do with node.js node.js is great for all I/O centric tasks Nearly all network apps are I/O centric (web or application server, socket server and so on)
node.js is good for... Web applications Streaming Unix tools (yeah, you can start your files with  #!/usr/bin/node )
Don't use it for... CPU intense apps with few I/O Apps that need huge memory (V8 has a 1.9G limit) Multiple cores need multiple processes (single threaded)
Modules you will fall in love with Express Socket.IO Cradle (CouchDB module)
There is room for improvement Tool support Debugging Error reporting Windows support (when you realy want a Windows server) XML support
JavaScript Support Node JS contains CommonsJS EcmaScript5 Usable node API
Integration Great support for NonSQL stores: CouchDB,  Redis But you still can use your RDB -> modules for all major databases
Resources http://nodejs.org/ https://github.com/joyent/node/wiki/modules http://expressjs.com/ http://socket.io/ You will need a good JavaScript understanding http://oreilly.com/catalog/9780596517748
Gerhard Hipfinger openForce Information Technology GesmbH Dresdner Str. 108 / 3. Stock / Top 11 1200 Wien TEL +43 1 3191775 FAX +43 1 3191775-20 http://openforce.com

node.js - Fast event based web application development

  • 1.
    node.js Fast eventbased web application development Wien, 7. April 2011 Gerhard Hipfinger
  • 2.
    What is node.js?A JavaScript runtime Based on Googles V8 JavaScript engine High performance async event handling Module system, I/O system bindings, supports all common network protocols
  • 3.
    Availability Available onall major platforms and on Windows too (via Cygwin or MinGW)
  • 4.
    Module Management Youwant to use module management as there are many available! Over 1100 NPM is your friend curl http://npmjs.org/install.sh | sh npm install express
  • 5.
    Obligatory Hello,World! console.log('Hello, node.js World!'); > node hello.js Hello, node.js World! Create file hello.js with the content: Than type:
  • 6.
    Design Goals Onlya small core library Extensible module system Non blocking I/O As close to system calls as possible
  • 7.
    Non Blocking I/OFirst we look at traditional blocking I/O I/O is serialized and the execution time is sum(opA, opB) var a = getSomethingFromDatabase(); c onsole.log(a); v ar b = getSomethingFromDatabase(); console.log(b);
  • 8.
    Non Blocking I/OgetSomethingFromDatabase(paramA, function(result) { console.log(result); }); getSomethingFromDatabase(paramB function(result) { console.log(result); }); Both queries are handled in an event queue and don't block each other Execution time is MAX(op1,op2)
  • 9.
    Non Blocking I/Onode.js uses epoll, kqueue or whatever is available at the underlying operating system So I/O does not waste your CPU cycles No busy waiting! Thread pool is used for everything else
  • 10.
    (No) Threads Therest of your app is single threaded Easy programming model but is not useful for all kinds of apps
  • 11.
    Your Webserver in6 LOC var http = require('http'); http.createServer( function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n'); }).listen(8888, "127.0.0.1"); console.log('Server running at http://127.0.0.1:8888/'); Pretty simple, isn't it?
  • 12.
    Ever needed topoll for filechange? var fs = require('fs'); fs.watchFile(__filename, function() { console.log('I was changed!'); process.exit(0); });
  • 13.
    So what canI do with node.js node.js is great for all I/O centric tasks Nearly all network apps are I/O centric (web or application server, socket server and so on)
  • 14.
    node.js is goodfor... Web applications Streaming Unix tools (yeah, you can start your files with #!/usr/bin/node )
  • 15.
    Don't use itfor... CPU intense apps with few I/O Apps that need huge memory (V8 has a 1.9G limit) Multiple cores need multiple processes (single threaded)
  • 16.
    Modules you willfall in love with Express Socket.IO Cradle (CouchDB module)
  • 17.
    There is roomfor improvement Tool support Debugging Error reporting Windows support (when you realy want a Windows server) XML support
  • 18.
    JavaScript Support NodeJS contains CommonsJS EcmaScript5 Usable node API
  • 19.
    Integration Great supportfor NonSQL stores: CouchDB, Redis But you still can use your RDB -> modules for all major databases
  • 20.
    Resources http://nodejs.org/ https://github.com/joyent/node/wiki/moduleshttp://expressjs.com/ http://socket.io/ You will need a good JavaScript understanding http://oreilly.com/catalog/9780596517748
  • 21.
    Gerhard Hipfinger openForceInformation Technology GesmbH Dresdner Str. 108 / 3. Stock / Top 11 1200 Wien TEL +43 1 3191775 FAX +43 1 3191775-20 http://openforce.com