becoming node.js ninja on cloud foundry

Download Becoming Node.js ninja on Cloud Foundry

Post on 20-Aug-2015




0 download

Embed Size (px)


  1. 1. Becoming a Node.js ninja onCloud FoundryRaja Rao DV (@rajaraodv)Cloud Foundry Developer Advocate (Node.js)March 2009 VMware Inc. All rights reserved
  2. 2. Agenda1. About Node.js Internal working of Node.js Buzz around Node.js Who is using it What kind of apps are being built2. Coding in Node.js Sync v/s Async coding (Callbacks) Classes & Modules (CommonJS) npm & package.json Node.js EventEmitters3. Node.js & Cloud Foundry (w/ demo) Hello World app in Cloud Foundry Using Sticky Sessions CloudFoundry Module & connecting to Redis, MongoDB etc. Express.js (RESTful) app + Express.js (Real-time) app2
  3. 3. About Node.jsNode.js is a platform to build fast and scalable network applications easily.It is built on Google Chromes v8 engine & implements event-driven, non-blocking I/O model.- It is ~80% C/C++ & ~20% JS (APIs)- Uses CommonJS module system.- Executes JavaScript on the server- Built by Ryan Dahl & sponsored by JoyentRyan Dahl(Node.js creator)3
  4. 4. What are the main advantages of Node.js? Provides savings in I/O cost to help build high-performanceservers Provides JavaScript interface build such servers quickly &easily4
  5. 5. The cost of I/O
  6. 6. So how does Node.js save I/O cost?Node.js saves I/O cost by implementing event driven, Non-blocking I/O model6
  7. 7. Event-driven, non-blocking I/O platform/serverWhat exactly is a event-driven, non-blocking platform/server?How is it different from a multi-threaded platform/server?Multi-threaded blocking serverv/s Event-driven, non-blocking server7
  8. 8. Multi-threaded server - Threads are spawned for every connection User1i/o request T1 DB User2i/o requestT2Blocking I/O User3 T3 T4 T5Refreshes 2 times User4T7 T8FS T6T9refreshes 3 timesMulti threadedserver T ThreadBecause every I/o is blocking, serverspawns a thread per connection* to supportmultiple requests 8
  9. 9. Non-blocking & Evented I/O (Node.js server)JSC/C++ T1User1i/o request V8 DB T1 Libio T1 V8 POSIXEvent loop V8AsyncUser2i/o request(Libev)Threads T1delegate i/o toNon-blocking t2I/O V8libeiot1 User3Refreshes 2 times Single t4thread t3servesT1t6all users V8t5 User4refreshes 3 timesT1t7 FS V8 i/o result returned T1 T1 2 EL after x time V8 T1V8 T1 JS Thread running V8 V8 your code (Single threaded) Node.js t1POSIX threads doing t2 async I/O (multi-threaded)t3 Everything except your (JS) code is runs in parallel (by libio) 9
  10. 10. Event-driven, non-blocking I/O serverReal-world example of the two models? Multi-threaded blocking server (Apache) v/s Event-driven, non-blocking server (Nginx) 10
  11. 11. Apache V/s Nginx: performanceReqs/sec v/s concurrent connectionsAt ~4000 concurrent connections,- Nginx can serve ~9000 reqs/sec- Apache can serve ~3000 reqs/secRef:
  12. 12. Apache V/s Nginx: Memory usageMemory v/s concurrent connectionsAt ~4000 concurrent connections,- Nginx uses 3MB memory- Apache uses 40MB memory Ref:
  13. 13. Saving I/O is great, what else is happening w/ Node.js? Lets look at community, libraries, buzz around Node.js13
  14. 14. Other things going on for Node.js 2nd most popular watched on git14
  15. 15. Other things going on for Node.js 8,000+ libraries/modules/serversHigh-level library categories Web frameworksOracle Multiple protocolsCommand Line Option Parsers Routers NoSQL and Key/ValueHTTPParser Generators MongoFTP Other Parsers Static file serversDebugging / Console Utilities Microframeworks Hive E-mailCompression RedisXMPP Frameworks Graphics CouchDBOther networkingSound Middleware Other NoSQL implementationsRPC Payment Gateways JSGIMiscellaneous and multiple DBWeb Sockets & AjaxAPI clients Connect Templating Message QueuesControl flow / Async goodies Other middlewareCSS EnginesClass systems I18n and L10n modules Other Content Management Systems Testing / Spec Frameworks Boilerplates DatabaseBuild and Deployment WrappersContinuous Integration Tools MS SQL Server Package Management Systems Parsers DDD, CQRS, EventSourcing Module LoaderJSONDesktop application related PostgreSQL JavaScript threads MySQL OpenSSL / Crypto / Hashing XMLOther SQLiteSMTP TCP / IP
  16. 16. Other things going on for Node.js Node in Production! LinkedIn, Yahoo!, Yammer, eBay, Twitter etc. >1000 other companies/startups are using it in production All kinds of interesting apps:End-user apps: Real-time apps Mobile apps CRMs, Web sites etc. etc. Platform apps (Servers / Services): Node-http-proxy - Node.js implementation of reverse proxy like nginx - Node.js implementation of LDAP server itself SMTP Node.js implementation of SMTP server itself XMPP, SSH, RPC, many more.16
  17. 17. Agenda part 21. About Node.js Internal working of Node.js Buzz around Node.js Who is using it What kind of apps are being built2. Coding in Node.js Sync v/s Async coding (Callbacks) Classes & Modules (CommonJS) npm & package.json Node.js EventEmitters3. Node.js & Cloud Foundry (w/ demo) Hello World app in Cloud Foundry Using Sticky Sessions CloudFoundry Module & connecting to Redis, MongoDB etc. Express.js (RESTful) app + Express.js (Real-time) app17
  18. 18. Lets look at the code..How does async code differ from sync(regular) code?Synchronous codev/s Asynchronous Code 18
  19. 19. Callbacks Control flow Use case: Lets say we have an items id and want to get its name from DB and print it//Synchronous & blocking code //Async & non-blocking codefunction getItemNameById(id) {function getItemNameById(id, callback) {//blocks or waits for DBdb.get(id, callback); //step 2return db.get(id); //step 2 //nothing is returned here} }var name = getItemNameById(100); //step 1 //step 3Some internal function calls the callback w/ result//print name in step 3console.log(name); //step 3 //You create a callback helper functionfunction displayHelperCallback(name) {console.log(name); //step 4}Things to note: //pass callback function to consume the result1. Async code doesnt directly return getItemNameById(100, displayHelperCallback); //step 1 anything2. Instead, it takes a function(callback) & calls that function when result becomes available19
  20. 20. Callbacks Control flow (detailed version in Node.js)//YOUR APPvar db = require(db);function getItemNameById(id, callback) { db.get(id, callback); //step 2}//You create a callback helper functionfunction displayHelperCallback(name) { console.log(name); //step 103}//pass callback function to consume the result//step 5getItemNameById(100, displayHelperCallback); //step 1 V8 is free to run other functions in the event-loop.//INTERNALS OF A DB LIBRARY (HIGH LEVEL)//step 5, step 6 ..step 100function db() { Say v8 notices 95 other things to do (in thethis.dbConnection = net.connection(); // connects to DBevent loop), it starts executing them one by}db.protorype.get = function(id, callback) { one. var self = this; //step 3 & //step4 is (below), function(result, callback) { Step 5 At some point b/w step 3 and step 100,self. receiveFromDB(result, callback);//step 101returns result & asks to run}); dbConnection.writes callback.}db.protorype.receiveFromDB = function(result, callback) { This event goes to the back of the queue ascallback(result); //Execute callback step step 102step 101}20
  21. 21. Node.js ProgrammingHow can I better organize my code? Classes & CommonJS module 21
  22. 22. JavaScript Classes (util.inherits) Node.js provides handy util.inherits function to inherit a class. - This also provides subclass.super_ to access super class functions var require(util); //import util module //Super Class function Automobile(license, model) { this.license = license; this.model = model; } Automobile.prototype.getModel = function() { return model; } //Sub class function Car(license, model) {, license, model); } util.inherits(Car, Automobile);22
  23. 23. CommonJS modules//Automobile.js fileThings to note:function Automobile(license, model) { 1. Allows keeping JS code inthis.license = license; separate filesthis.model = model;}2. Use exports. to exportAutomobile.prototype.getModel = function() { somethingreturn model;} 1. Use require(path/to/module) toexports.Automobile = Automobile; import it2. use require(module). to//Car.js file access things inside modulevar util = require(util);var module = require(./Automobile);var Automobile = module.Automobile;function Car(license, model) {, license, model);}util.inherits(Car, Automobile);console.log(new Car("1232", "BMW").model); //prints BMW23
  24. 24. CommonJS modules: Exporting multiple things//myModule.js file Things to note:exports.myFunction = function () { 1. You can directly export function,return hi there;arrays, variables}exports.myArray = [foo, bar]; 2. You can export multiple thingsexports.myVariable = Im a variable;from one file using exports//app.js filevar myModule = require(./myModule);console.log(myModule.myFunction()); //prints hi thereconsole.log(myModule.myArray[1]); //prints barconsole.log(myModule.myVariable); //prints Im a variable24
  25. 25. CommonJS modules: exports v/s module.exports//myModule.js file Things to note:module.exports = function () { If you want to export only onereturn hi there; class/function.. so that it can be} used directly by the recipient, you can use: module.exports = ; Warning: If you use both module.exports//app.js filevar myFunction = require(./myModule); and exports.bla, exports.bla will NOT be exported(ignored)console.log(myModule.myFunction()); //prints hi there