0
HOW TO                     WRITE                     BIG APPS.                     Richard Rodger                     @rjr...
Node.js apps                     are getting                     bigger.Friday 26 October 2012
So, how do                     you scale?                     ‣be stateless,                     ‣lots of little processes...
NOFriday 26 October 2012
How do you                         scale this?Friday 26 October 2012
Lets write a                     class for                     Cars.Friday 26 October 2012
Classical          function Car( params ) {            this.color     = params.color       ||   "red"            this.marq...
Classical          function Car( params ) {            this.color     = params.color       ||   "red"            this.marq...
var Car = {                         Prototypical            color:        "red",            marque:       "Ferrari",      ...
var Car = {                         Prototypical            color:        "red",            marque:       "Ferrari",      ...
Functional          function Car( params ) {            var self = {}               var       color       =   params.color...
Functional          function Car( params ) {            var self = {}               var       color       =   params.color...
Which one                     should we                     use for                     Cars?Friday 26 October 2012
Lets play a                     GAME.Friday 26 October 2012
Stand up.Friday 26 October 2012
Stand up.                     Everybody.Friday 26 October 2012
Is this a car?Friday 26 October 2012
YES       NO        Stay standing.           Sit down.                               And stay down!Friday 26 October 2012
Is this a car?Friday 26 October 2012
Is this a car?Friday 26 October 2012
Is this a car?Friday 26 October 2012
Is this a car?Friday 26 October 2012
Is this a car?Friday 26 October 2012
Is this a car?Friday 26 October 2012
Is this a car?                     Its a painting.Friday 26 October 2012
Which one?                     ‣Classical                     ‣Prototypical                     ‣FunctionalFriday 26 Octob...
Which one?                     ‣Classical                     ‣Prototypical                     ‣Functional               ...
Whats wrong                     with properties?                     ‣Color - Lego?                     ‣Marque - Batman b...
Classes                     are                     ideal                     Platonic                     forms.Friday 26...
Think of the ideal                     triangle.                     ‣Sides - 3!                     ‣Points - 3!         ...
Think of the ideal                     triangle.                     ‣Sides - 3!                     ‣Points - 3!         ...
Plato was                     wrong.                     Ideal forms do                     not exist.Friday 26 October 2012
George                     Berkeley                     berkeley.edu                     same guy                     also...
Inventor of...Friday 26 October 2012
Friday 26 October 2012
There is no                     mental image                     of a ideal                     triangle.Friday 26 October...
Classes suck.                     Objects are ok.Friday 26 October 2012
Classes suck.                     Objects are ok.                     i nh                          eritan ceFriday 26 Oct...
Classes suck.                       refac                              torin                     Objects are ok.          ...
Classes suck.                       refac                              torin                                       archi  ...
on                     Classes suck.                                                           lati                       ...
on                     Classes suck.                                 sch                                                  ...
on                     Classes suck.                                 sch                                                  ...
on                     Classes suck.                                 sch                                                  ...
on                     Classes suck.                                 sch                                                  ...
Something is                     wrong with                     modeling a                     car.Friday 26 October 2012
But you can                     still drive all                     those cars.Friday 26 October 2012
Its not what                     you are.                     Its what you                     do.Friday 26 October 2012
What does your                     app want to do?                     ‣Register a new user,                     ‣Place an...
This is JavaScript          JSON           function             JSON                                            OUTPUT,   ...
How do you find the          right "thing to do"?Friday 26 October 2012
How do you find the          right "thing to do"?          PATTERN          MATCHING!Friday 26 October 2012
{                         thing: "product",                         cmd:   "save",                         ... other field...
Define your          "business logic" in          terms of these          commands.Friday 26 October 2012
What do you get as          the pay off?Friday 26 October 2012
Really easy testing.          Verify the returned          JSON and youre          done.Friday 26 October 2012
A distributed system,          if you need it.          That gives you scale          too.Friday 26 October 2012
Completely          decoupled          commands that never          need to know about          each other.Friday 26 Octob...
Plugins for free - just          bundle up a few          related commands.          Organize your code!Friday 26 October ...
middleware-style          layering - e.g. add a          caching layer by          matching data          storage commands...
Stay in control -          tracing, logging,          throttling,          permissions, special          cases - all easy....
Documentation - just          list all the commands          and the JSON they          expect.Friday 26 October 2012
Code!          Open sourced at          http://senecajs.org          npm install senecaFriday 26 October 2012
Define and call a command.          var seneca = require(seneca)()          // define a command          seneca.add({role:...
Pinning an API for convenience          // define a command          seneca.add({role:math, cmd:sum},                     ...
Plugins: commands + HTTP API.          seneca.use( function(seneca,options,callback) {            seneca.add( {role:math, ...
Build a distributed system          // client:          // use transport plugin to calculate product remotely          sen...
Data storage (also plugin-based)          var product = seneca.make(product)          product.name = apple          produc...
Also, seneca...                     ‣is about 18 months old                     ‣has detailed tracing                     ...
Why did we built it?                     builds          Minimum Viable          Products that          become big apps.Fr...
senecajs.org          @nodeseneca          Thanks!Friday 26 October 2012
Upcoming SlideShare
Loading in...5
×

How to Write Big Apps (Richard Rodger NodeDublin 2012)

1,395

Published on

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

  • Be the first to like this

No Downloads
Views
Total Views
1,395
On Slideshare
0
From Embeds
0
Number of Embeds
1
Actions
Shares
0
Downloads
29
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

Transcript of "How to Write Big Apps (Richard Rodger NodeDublin 2012)"

  1. 1. HOW TO WRITE BIG APPS. Richard Rodger @rjrodgerFriday 26 October 2012
  2. 2. Node.js apps are getting bigger.Friday 26 October 2012
  3. 3. So, how do you scale? ‣be stateless, ‣lots of little processes, ‣funky V8 options, ‣...Friday 26 October 2012
  4. 4. NOFriday 26 October 2012
  5. 5. How do you scale this?Friday 26 October 2012
  6. 6. Lets write a class for Cars.Friday 26 October 2012
  7. 7. Classical function Car( params ) { this.color = params.color || "red" this.marque = params.marque || "Ferrari" this.model = params.model || "Testarossa" this.cylinders = params.cylinders || 12 this.timeto60 = params.timeto60 || 5.2 // seconds } Car.prototype.drive = function() { console.log( "vrooom!" ) } var car = new Car({color:"yellow"}) car.drive()Friday 26 October 2012
  8. 8. Classical function Car( params ) { this.color = params.color || "red" this.marque = params.marque || "Ferrari" this.model = params.model || "Testarossa" this.cylinders = params.cylinders || 12 this.timeto60 = params.timeto60 || 5.2 // seconds } Car.prototype.drive = function() { console.log( "vrooom!" ) } VO TE var car = new Car({color:"yellow"}) NO car.drive() WFriday 26 October 2012
  9. 9. var Car = { Prototypical color: "red", marque: "Ferrari", model: "Testarossa", cylinders: 12, timeto60: 5.2, drive: function() { console.log( "vrooom!", this ) } } var car = Object.create(Car) car.color = "yellow" car.drive()Friday 26 October 2012
  10. 10. var Car = { Prototypical color: "red", marque: "Ferrari", model: "Testarossa", cylinders: 12, timeto60: 5.2, drive: function() { console.log( "vrooom!", this ) } } VO TE var car = Object.create(Car) NO car.color = "yellow" car.drive() WFriday 26 October 2012
  11. 11. Functional function Car( params ) { var self = {} var color = params.color || "red" var marque = params.marque || "Ferrari" var model = params.model || "Testarossa" var cylinders = params.cylinders || 12 var timeto60 = params.timeto60 || 5.2 self.drive = function() { console.log( "vrooom!", self ) } return self } var car = Car({color:"yellow"}) car.drive()Friday 26 October 2012
  12. 12. Functional function Car( params ) { var self = {} var color = params.color || "red" var marque = params.marque || "Ferrari" var model = params.model || "Testarossa" var cylinders = params.cylinders || 12 var timeto60 = params.timeto60 || 5.2 self.drive = function() { console.log( "vrooom!", self ) VO TE } return self NO } var car = Car({color:"yellow"}) W car.drive()Friday 26 October 2012
  13. 13. Which one should we use for Cars?Friday 26 October 2012
  14. 14. Lets play a GAME.Friday 26 October 2012
  15. 15. Stand up.Friday 26 October 2012
  16. 16. Stand up. Everybody.Friday 26 October 2012
  17. 17. Is this a car?Friday 26 October 2012
  18. 18. YES NO Stay standing. Sit down. And stay down!Friday 26 October 2012
  19. 19. Is this a car?Friday 26 October 2012
  20. 20. Is this a car?Friday 26 October 2012
  21. 21. Is this a car?Friday 26 October 2012
  22. 22. Is this a car?Friday 26 October 2012
  23. 23. Is this a car?Friday 26 October 2012
  24. 24. Is this a car?Friday 26 October 2012
  25. 25. Is this a car? Its a painting.Friday 26 October 2012
  26. 26. Which one? ‣Classical ‣Prototypical ‣FunctionalFriday 26 October 2012
  27. 27. Which one? ‣Classical ‣Prototypical ‣Functional NONE!Friday 26 October 2012
  28. 28. Whats wrong with properties? ‣Color - Lego? ‣Marque - Batman brand? ‣Model - Flintstones? ‣Cylinders - Star Wars hover car? ‣timeto60 - Model T max is 45mphFriday 26 October 2012
  29. 29. Classes are ideal Platonic forms.Friday 26 October 2012
  30. 30. Think of the ideal triangle. ‣Sides - 3! ‣Points - 3! ‣Total of Angles - 180°!Friday 26 October 2012
  31. 31. Think of the ideal triangle. ‣Sides - 3! ‣Points - 3! ‣Total of Angles - 180°! Now picture it in your mind.Friday 26 October 2012
  32. 32. Plato was wrong. Ideal forms do not exist.Friday 26 October 2012
  33. 33. George Berkeley berkeley.edu same guy also Irish!Friday 26 October 2012
  34. 34. Inventor of...Friday 26 October 2012
  35. 35. Friday 26 October 2012
  36. 36. There is no mental image of a ideal triangle.Friday 26 October 2012
  37. 37. Classes suck. Objects are ok.Friday 26 October 2012
  38. 38. Classes suck. Objects are ok. i nh eritan ceFriday 26 October 2012
  39. 39. Classes suck. refac torin Objects are ok. i nh eritanc egFriday 26 October 2012
  40. 40. Classes suck. refac torin archi tectu Objects are ok. itanc eg re er i nhFriday 26 October 2012
  41. 41. on Classes suck. lati archi u aps refac tectu enc torin Objects are ok. itanc eg re er i nhFriday 26 October 2012
  42. 42. on Classes suck. sch lati e archi ma u s aps refac tectu enc torin Objects are ok. itanc eg re er i nhFriday 26 October 2012
  43. 43. on Classes suck. sch lati e archi ma u s ism aps refac tectu orph enc torin Objects are ok. itanc eg polym re er i nhFriday 26 October 2012
  44. 44. on Classes suck. sch lati e archi ma un u s ism aps it t refac tectu orph enc est torin Objects are ok. eg ing itanc polym re er i nhFriday 26 October 2012
  45. 45. on Classes suck. sch lati e archi ma un u s ism aps it t refac tectu orph enc est torin Objects are ok. eg encies ing itanc pen d polym re er de i nhFriday 26 October 2012
  46. 46. Something is wrong with modeling a car.Friday 26 October 2012
  47. 47. But you can still drive all those cars.Friday 26 October 2012
  48. 48. Its not what you are. Its what you do.Friday 26 October 2012
  49. 49. What does your app want to do? ‣Register a new user, ‣Place an order, ‣Call an external service, ‣Save and load data, ‣...Friday 26 October 2012
  50. 50. This is JavaScript JSON function JSON OUTPUT, A command asynchronously, INPUT for your app, of course (mostly in- that memory objects) "does stuff"Friday 26 October 2012
  51. 51. How do you find the right "thing to do"?Friday 26 October 2012
  52. 52. How do you find the right "thing to do"? PATTERN MATCHING!Friday 26 October 2012
  53. 53. { thing: "product", cmd: "save", ... other fields doSave() } { thing: "product", cmd: "load", ... other fields doLoad() }Friday 26 October 2012
  54. 54. Define your "business logic" in terms of these commands.Friday 26 October 2012
  55. 55. What do you get as the pay off?Friday 26 October 2012
  56. 56. Really easy testing. Verify the returned JSON and youre done.Friday 26 October 2012
  57. 57. A distributed system, if you need it. That gives you scale too.Friday 26 October 2012
  58. 58. Completely decoupled commands that never need to know about each other.Friday 26 October 2012
  59. 59. Plugins for free - just bundle up a few related commands. Organize your code!Friday 26 October 2012
  60. 60. middleware-style layering - e.g. add a caching layer by matching data storage commands.Friday 26 October 2012
  61. 61. Stay in control - tracing, logging, throttling, permissions, special cases - all easy.Friday 26 October 2012
  62. 62. Documentation - just list all the commands and the JSON they expect.Friday 26 October 2012
  63. 63. Code! Open sourced at http://senecajs.org npm install senecaFriday 26 October 2012
  64. 64. Define and call a command. var seneca = require(seneca)() // define a command seneca.add({role:math, cmd:sum}, function(args,callback) { var sum = args.left + args.right callback(null,{answer:sum}) }) // call the command seneca.act({role:math, cmd:sum, left:1, right:2}, function(err,result) { if( err ) return console.error( err ) console.log(result) }) // prints: { answer: 3 }Friday 26 October 2012
  65. 65. Pinning an API for convenience // define a command seneca.add({role:math, cmd:sum}, function(args,callback) { var sum = args.left + args.right callback(null,{answer:sum}) }) // pin the API var math = seneca.pin({role:math,cmd:*}) // call the command math.sum({left:1,right:2}, function(err,result) { console.log(result) }) // prints: { answer: 3 }Friday 26 October 2012
  66. 66. Plugins: commands + HTTP API. seneca.use( function(seneca,options,callback) { seneca.add( {role:math, cmd:sum}, function(args,callback) { var sum = args.left + args.right callback(null,{answer:sum}) }) seneca.add( {role:math, cmd:product}, function(args,callback) { var product = args.left * args.right callback(null,{answer:product}) }) callback( null, seneca.http({ // just a connect middleware pin: {role:math, cmd:*}, map: { sum: {}, product: {} }, // GET is the default HTTP method args: { left: parseFloat, right: parseFloat } })) }) var app = connect() .use(connect.query()) .use(seneca.service()) .listen(3000) // http://localhost:3000/api/sum?left=1&right=2Friday 26 October 2012
  67. 67. Build a distributed system // client: // use transport plugin to calculate product remotely seneca.use(transport,{ pins:[ {role:math, cmd:product} ] }) // this will go out over network seneca.act({role:math, cmd:product, left:3, right:4}, function(err,result) { if( err ) return console.error( err ) console.log(result) }) // server (as per previous slide): seneca.use(transport) ... app.use( connect.json() ) ...Friday 26 October 2012
  68. 68. Data storage (also plugin-based) var product = seneca.make(product) product.name = apple product.price = 100 // ActiveRecord-style product.save$(function( err, product ) { if( err ) return console.error( err ) console.log( saved: +product ) // product.id was generated for you product.load$({id:product.id},function( err, product ) { if( err ) return console.error( err ) console.log( loaded: +product ) }) }) // output: // saved: //product:{id=286624;name=apple;price=100} // loaded: //product:{id=286624;name=apple;price=100} // mix-and-match in-memory, MongoDB, MySQL, PostreSQL, etc.Friday 26 October 2012
  69. 69. Also, seneca... ‣is about 18 months old ‣has detailed tracing ‣has nice error messages ‣has a REPL! @dshaw happy? ‣is used in productionFriday 26 October 2012
  70. 70. Why did we built it? builds Minimum Viable Products that become big apps.Friday 26 October 2012
  71. 71. senecajs.org @nodeseneca Thanks!Friday 26 October 2012
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×