SC5 Meetup: Node.js night with Jaakko Manninen


Published on

Jaakko Manninen's presentation on Node.js Lesson Learned at SC5 Meetup

Published in: Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide

SC5 Meetup: Node.js night with Jaakko Manninen

  1. 1. Node.js at Rocket PackLessons learned; Patterns to know and loveJaakko ManninenRocket Pack / Disney InteractiveWednesday, May 15, 13
  2. 2. Rocket Pack• Makes awesome HTML5 games and the tools tobuild them• Engine,Tool chain, Services 100% JS• Acquired by Disney in 2011• Started using Node.js in 2010, version 0.1• Full stack, cloud hosted setup• Very early days for HTML5 games: first toolchainWednesday, May 15, 13
  3. 3. What is Node.js• JavaScript engine with IO library & APIs• Google’sV8 + libuv + core APIs• V8 is Chrome’s very fast JS engine• One thread of execution• Asynch, non-blocking, event driven IO• Great for servers, real-time messaging, butalso for web and standalone appsWednesday, May 15, 13
  4. 4. Road to Node• First as HTTP front end for Scala backend• Parse request, deliver to AMQP message broker• Games ran engine code in Rhino/JVM• Ran it in Node and soon ported everythingto Node• V8 is very fast :)• Most packages we have used, we have had to patch :(• Today package quality improving but still poor :/Wednesday, May 15, 13
  5. 5. Rocket Pack version 1• Cloud: Scalable, stateless, modular• Top to bottom JavaScript, all the way fromMongoDB to the client side runtime engine• Messaging to tie the elements together• First RabbitMQ & node-amqp• Then wrote our own Messaging APIWednesday, May 15, 13
  6. 6. Messaging• Wrote load-balancing, p2p messaging APIusing• Careful about fanout style exchanges, where processing capability islimited to one core only• Central discovery: if you have a multicast group, think about node-mdns• Socket.IO messages quickly routed toservices by FE• Sticky lb; no Store yet; no yet• Use with eg. a Redis StoreWednesday, May 15, 13
  7. 7. Processing woes• An evolving platform needs to run data migrations• Memory limited to 1.2GB (fixed only in 2012)• Use cluster to task workers on other cores• Or discover nodes in cluster (mdns, roll-your-own)Wednesday, May 15, 13
  8. 8. Lessons part 1• One-thread-only is your friend in scaling• Many packages are buggy and you will haveto patch them (but submit a pull request!)• Use domain to capture out-of-stack errors• Where did that socket hang up come from? domain will tell you• Domain itself is experimental• Never throw - just return errors• Never use uncaughtExceptionWednesday, May 15, 13
  9. 9. Part 2 - Local Tools• Brought dev tools to the dev’s machine• Windows support; offline support• Plugins: declare and depend on namedinterfaces• Excellent testability, modularity, composability• CommonJS, npm, package.json everywhereWednesday, May 15, 13
  10. 10. • npm is an awesome package manager• Large dependency trees may cause rare issues• It doesn’t have to be JavaScript• Tens of thousands of packages• Private repository recommended• Our repo mirrors npm; has our private packages• Not trivial to set up, but worth itWednesday, May 15, 13
  11. 11. Wednesday, May 15, 13
  12. 12. browserify• your CommonJS modules in abundle loadable in the browser• connect + browserify = JIT compilation• Manage your site/app’s dependencies vianpm• Most Node APIs available• Even our UI is a browserified npm pluginWednesday, May 15, 13
  13. 13. when.js• is a lightweight CommonJSPromises/A+ library with other async goodies(iterators etc)• We now use Promises everywhere• used to use• Bubbling up errors: handle errors in one place• Also wraps Node functions in Promises•,‘/etc/passwd’).then()Wednesday, May 15, 13
  14. 14. return longTask().then(function(result) {// do stuff with result}).otherwise(function(error) {})longTask(function(err, result) {if (err) return err;// do stuff with result})Without Promises:Bubble up Promises:Wednesday, May 15, 13
  15. 15. vfs• is aVirtual File Systemabstraction that works over WS (with msgpack),HTTP and locally in Node• brings the same FS API to the whole stack; lets usaccess the fs the same way everywhere; browserto server• uses, an RPC agent systemthat gives us streams and processes in the browser• output logs from long-running processes• EMFILE: file watching by interval not by fd;, May 15, 13
  16. 16. Lessons part 2• Always use jshint in your editor• Forget callbacks, use and return Promises• Use browserify and npm to assemble yoursoftware• Design around one thread per process andpartition around ~3500m/s type of issues• Build with grunt; test with mocha andjsdom• jsdom is fully fledged DOM implementationWednesday, May 15, 13
  17. 17. Native applications• letsyou build native apps with Node.js andWebKit• Linux, Mac,Windows all supported• Even WebGL is supported• TPOLM demo time! (wrapped in node-webkit)•, May 15, 13
  18. 18. Thanks!, May 15, 13