PLAYING WITH FIRE  (ROCKET FUEL ACTUALLY)    An introduction to node.js
Mike Hagedorn                  Anthony Broussard   @mwhagedorn                      @quantumpotatocodemav.com/mwhagedorn  ...
IN THE BEGINNING...    Server-side Javascript
IN THE BEGINNING...    Server-side Javascript          (it sucked)
SERVER-SIDE JAVASCRIPT    •Netscape Livewire(1996)    •Rhino(1997)    •Several others since(like 50)
Y SO BAD?•Slow Engines•Javascript’s Perception (until recently)•Much better alternatives
Y IZ BETTR NAO? Lots of Competition •SpiderMonkey •JavascriptCore •Chakra                   Javascript is cool now!
WHAT IS NODE.JS?•Created By Ryan Dahl•Google’s V8 Engine (No DOM)•Uses Nonblocking I/O•Single Threaded•A New Way To Build ...
WHY SHOULD YOU CARE?                            •Its Fast  > summary(node1$ttime)      Min. 1st Qu.    Median      Mean   ...
WHY SHOULD YOU CARE?•It can handle LOTS of concurrent transactions
WHY SHOULD YOU CARE?  • Makes near real time things easy  • Its small  • Its Javascript   •(Second most used language on G...
WHO’S USING ITOthers:   http://doiop.com/rocket-node
HELLO WORLDserver.js
HELLO WORLDserver.js setTimeout(function(){  console.log(“world”) },2000); console.log(“hello”);
HELLO WORLDserver.js setTimeout(function(){  console.log(“world”) },2000); console.log(“hello”);            $ node server....
HELLO WORLDserver.js setTimeout(function(){  console.log(“world”) },2000); console.log(“hello”);                          ...
NON-BLOCKING I/O (asynchronous is the new black)
BLOCKING I/Ovar a = db.query(SELECT A);console.log(result a:, a);var b = db.query(SELECT B);console.log(result b:, b);    ...
NON-BLOCKING I/Odb.query(SELECT A, function(result) {  console.log(result a:, result);});db.query(SELECT B, function(resul...
SINGLE THREADED!               You have to use callbacks!db.query(SELECT A, function(result) {    object.mySlowCall(result...
WHY ISN’T EVERYONE USING  NON-BLOCKING I/O?  There are cultural and infrastructural                  reasons
CULTURAL BIASWe’re taught I/O with this:puts(“Enter your name:”)var name = gets()We’re taught to demand input and do      ...
CULTURAL BIASThis code:puts(“Enter your name:”)var name = gets(function(name){   puts(“Name: “)+name);})is rejected as TOO...
MISSING INFRASTRUCTURESo why isn’t everyone using event loops?Single threaded event loops require I/O to be non blockingMo...
OTHER APPROACHES?•Twisted•EventMachine•Others•Have lots of blocking libs to contend with•From the start Node has never pro...
JAVASCRIPT...•Has had event loops from the beginning•Anonymous functions, closures•Single threaded•The culture of Javascri...
GREAT FOR•Single Page Apps•Realtime updates•Processors/Crawlers•Process Monitoring•File Uploading
INSTALLINGOSX $ brew install nodeLinux$ git clone .... $ configure $ makeWindows  Not Yet (version 0.6)
INSTALLING NPM  •Node Package Manager  •Similar to RubyGems, Python easy_install$ curl http://npmjs.org/install.sh | sh
COMMON JS MODULEShello.js   exports.world = function(){     return “Hello World”;   }main.js var hello = require(‘./hello’...
EVENTS {EventEmitter} = require ‘events’ emitter = new EventEmitter emitter.on ‘foo’, -> console.log ‘bar’ emitter.emitusu...
QUEUEING AN EVENTfunction longFunction(){};process.nextTick(longFunction);Call longfunction on next time through event loop
EMITTING AN EVENTvar events = require(events);var eventEmitter = new events.EventEmitter();eventEmitter.on(someOccurence, ...
MANAGING ASYNCHRONCITY   A        B         C         use async!
MANAGING ASYNCHRONCITY        A                    B                        Cvar operation = function(a_data, b_data, call...
MANAGING ASYNCHRONCITY                 A                                               C                 Bvar operation = ...
EXPRESS WEB FRAMEWORKvar app = express.createServer();app.get(/, function(req, res){    res.send(Hello World);});app.liste...
COFFEESCRIPT•“Little language that compiles to javascript”•“It’s just javascript”• More ruby-like
COFFEESCRIPT  •“Little language that compiles to javascript”  •“It’s just javascript”  • More ruby-likesquare = (x) -> x * x
COFFEESCRIPT  •“Little language that compiles to javascript”  •“It’s just javascript”  • More ruby-likesquare = (x) -> x *...
DEMO
QUESTIONS?                  http://spkr8.com/t/8178   Mike Hagedorn                Anthony Broussard    @mwhagedorn       ...
Playing With Fire - An Introduction to Node.js
Upcoming SlideShare
Loading in …5
×

Playing With Fire - An Introduction to Node.js

2,109 views

Published on

node.js is an evented server-side Javascript framework powered by the Google V8 Javascript engine. It is a platform ideal for creating highly scalable web applications. It has the same simplicity of frameworks such as Sinatra, but is designed to be more peformant from the ground up. This performance is achieved by making all network I/O non blocking and all file I/O asynchronous. We will go over how that impacts the development experience, and walk through a simple web application. Javascript is foundational to this type of I/O because it is already evented by design. We will also take a brief look a similar evented frameworks such as ruby`s EventMachine.

Published in: Technology
0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,109
On SlideShare
0
From Embeds
0
Number of Embeds
22
Actions
Shares
0
Downloads
53
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • lunch counter example\n\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • npm doesnt run at all on win due to a lack of compatible child process forking\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Playing With Fire - An Introduction to Node.js

    1. 1. PLAYING WITH FIRE (ROCKET FUEL ACTUALLY) An introduction to node.js
    2. 2. Mike Hagedorn Anthony Broussard @mwhagedorn @quantumpotatocodemav.com/mwhagedorn codemav.com/quantumpotatomike@silverchairsolutions.com anthony@chaione.com
    3. 3. IN THE BEGINNING... Server-side Javascript
    4. 4. IN THE BEGINNING... Server-side Javascript (it sucked)
    5. 5. SERVER-SIDE JAVASCRIPT •Netscape Livewire(1996) •Rhino(1997) •Several others since(like 50)
    6. 6. Y SO BAD?•Slow Engines•Javascript’s Perception (until recently)•Much better alternatives
    7. 7. Y IZ BETTR NAO? Lots of Competition •SpiderMonkey •JavascriptCore •Chakra Javascript is cool now!
    8. 8. WHAT IS NODE.JS?•Created By Ryan Dahl•Google’s V8 Engine (No DOM)•Uses Nonblocking I/O•Single Threaded•A New Way To Build Scalable Network Platforms
    9. 9. WHY SHOULD YOU CARE? •Its Fast > summary(node1$ttime) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.0000 0.0000 1.0000 0.7437 1.0000 106.0000 > summary(thin1$ttime) Min. 1st Qu. Median Mean 3rd Qu. Max. 0.000 1.000 1.000 1.122 1.000 74.000 > summary(narwhal1$ttime) Min. 1st Qu. Median Mean 3rd Qu. Max. 15.00 22.00 23.00 23.74 24.00 88.00 > summary(v8cgi1$ttime) Min. 1st Qu. Median Mean 3rd Qu. Max. 12.00 13.00 13.00 14.49 18.00 39.00
    10. 10. WHY SHOULD YOU CARE?•It can handle LOTS of concurrent transactions
    11. 11. WHY SHOULD YOU CARE? • Makes near real time things easy • Its small • Its Javascript •(Second most used language on Github)
    12. 12. WHO’S USING ITOthers: http://doiop.com/rocket-node
    13. 13. HELLO WORLDserver.js
    14. 14. HELLO WORLDserver.js setTimeout(function(){ console.log(“world”) },2000); console.log(“hello”);
    15. 15. HELLO WORLDserver.js setTimeout(function(){ console.log(“world”) },2000); console.log(“hello”); $ node server.js hello world
    16. 16. HELLO WORLDserver.js setTimeout(function(){ console.log(“world”) },2000); console.log(“hello”); print(“hello”); sleep(2000); print(“world”); $ node server.js hello world
    17. 17. NON-BLOCKING I/O (asynchronous is the new black)
    18. 18. BLOCKING I/Ovar a = db.query(SELECT A);console.log(result a:, a);var b = db.query(SELECT B);console.log(result b:, b); Time = SUM(A, B)
    19. 19. NON-BLOCKING I/Odb.query(SELECT A, function(result) { console.log(result a:, result);});db.query(SELECT B, function(result) { console.log(result b:, result);}); Time = MAX(A, B)
    20. 20. SINGLE THREADED! You have to use callbacks!db.query(SELECT A, function(result) { object.mySlowCall(result, function(){ console.log(“my result”); })});
    21. 21. WHY ISN’T EVERYONE USING NON-BLOCKING I/O? There are cultural and infrastructural reasons
    22. 22. CULTURAL BIASWe’re taught I/O with this:puts(“Enter your name:”)var name = gets()We’re taught to demand input and do nothing until we have it.
    23. 23. CULTURAL BIASThis code:puts(“Enter your name:”)var name = gets(function(name){ puts(“Name: “)+name);})is rejected as TOO COMPLICATED
    24. 24. MISSING INFRASTRUCTURESo why isn’t everyone using event loops?Single threaded event loops require I/O to be non blockingMost libraries are not.
    25. 25. OTHER APPROACHES?•Twisted•EventMachine•Others•Have lots of blocking libs to contend with•From the start Node has never provided a blocking API•Its a clean slate•These approaches can be hard to use
    26. 26. JAVASCRIPT...•Has had event loops from the beginning•Anonymous functions, closures•Single threaded•The culture of Javascript embraces eventedprogramming
    27. 27. GREAT FOR•Single Page Apps•Realtime updates•Processors/Crawlers•Process Monitoring•File Uploading
    28. 28. INSTALLINGOSX $ brew install nodeLinux$ git clone .... $ configure $ makeWindows Not Yet (version 0.6)
    29. 29. INSTALLING NPM •Node Package Manager •Similar to RubyGems, Python easy_install$ curl http://npmjs.org/install.sh | sh
    30. 30. COMMON JS MODULEShello.js exports.world = function(){ return “Hello World”; }main.js var hello = require(‘./hello’); var sys = require(‘sys’); sys.puts(hello.world()); $ node main.js #Hello World
    31. 31. EVENTS {EventEmitter} = require ‘events’ emitter = new EventEmitter emitter.on ‘foo’, -> console.log ‘bar’ emitter.emitusual new_monster event. And check out how much nicer our emitting the ‘foo’ dependency graph has become!http://pragprog.com/magazines/2011-08/content Imagine No Dependencies A lot of Node developers will tell you that attaching things to global rather than exports is a no-no. And if you’re packaging your code as a library, or trying to make your code reusable, then they’re right. But if you’re developing a standalone application, then go ahead and let yourself declare a few globals.
    32. 32. QUEUEING AN EVENTfunction longFunction(){};process.nextTick(longFunction);Call longfunction on next time through event loop
    33. 33. EMITTING AN EVENTvar events = require(events);var eventEmitter = new events.EventEmitter();eventEmitter.on(someOccurence, function(message){ console.log(message);});eventEmitter.emit(someOccurence, Something happened!);
    34. 34. MANAGING ASYNCHRONCITY A B C use async!
    35. 35. MANAGING ASYNCHRONCITY A B Cvar operation = function(a_data, b_data, callback){ async.series([ function(as_callback), function(as_callback), function(err,results){ //[resulta, resultb] } ]); }
    36. 36. MANAGING ASYNCHRONCITY A C Bvar operation = function(a_data, b_data, callback){ async.parallel([ function(as_callback), function(as_callback), function(err,results){ //[resulta, resultb] } ]); }
    37. 37. EXPRESS WEB FRAMEWORKvar app = express.createServer();app.get(/, function(req, res){ res.send(Hello World);});app.listen(3000);
    38. 38. COFFEESCRIPT•“Little language that compiles to javascript”•“It’s just javascript”• More ruby-like
    39. 39. COFFEESCRIPT •“Little language that compiles to javascript” •“It’s just javascript” • More ruby-likesquare = (x) -> x * x
    40. 40. COFFEESCRIPT •“Little language that compiles to javascript” •“It’s just javascript” • More ruby-likesquare = (x) -> x * xsquare = function(x){ return x * x;}
    41. 41. DEMO
    42. 42. QUESTIONS? http://spkr8.com/t/8178 Mike Hagedorn Anthony Broussard @mwhagedorn @quantumpotatomike@silverchairsolutions.com anthony@chaione.com

    ×