Page 1
© 2009 VMware Inc. All rights reserved
Becoming a Node.js ninja onCloud Foundry
March 2012
Patrick Chanezon, Senior Director, Developer [email protected] , @chanezonWith slides from Raja Rao DV
www.cloudfoundry.com
Wednesday, March 28, 12
Page 2
P@ in a nutshell
• French, based in San Francisco
• Senior Director, Developer Relations, VMware
• Software Plumber, API guy, mix of Enterprise and Consumer
• 18 years writing software, backend guy with a taste for javascript
• 2 y Accenture (Notes guru), 3 y Netscape/AOL (Servers, Portals), 5 y Sun (ecommerce, blogs, Portals, feeds, open source)
• 6 years at Google, API guy (first hired, helped start the team)
• Adwords, Checkout, Social, HTML5, Cloud
Wednesday, March 28, 12
Page 3
3
Sketching Things
Slender clouds. On the pavilion a small rain.Noon, but I’m too lazy to open the far cloister.I sit looking at moss so green my clothes are soaked with color.
Wang Wei (699-759)
Wednesday, March 28, 12
Page 4
4
Taking a Nap by a Mountain Window
Resting my body in a monk’s cloud chamber, my dreams relax.Pine trees and cranes rise between screen and pillow.A beautiful pheasant makes a long song.My hand pushes the window, and the mountain fill my eyes.
Zhu Yunming (1461-1527)
Wednesday, March 28, 12
Page 5
5
Looking for the Hermit and Not Finding Him
Beneath a pine I question a boy.He says, “Master has gone to gather herbs somewhere on the mountainbut who knows where? The clouds are deep.”
Jiao Dao (778-841)
Wednesday, March 28, 12
Page 6
6
Looking for the Hermit and Not Finding Him
Lu mountain stands beside the Southern DipperIn clouds reaching silken like a nine-panelled screenWith its shadows in a crystal lake deepening the green water.The Golden Gate opens into two mountain ranges.
Li Bai (701-762)
Wednesday, March 28, 12
Page 7
7
Agenda
1. About Node.js• Internal working of Node.js• Buzz around Node.js• Who is using it• What kind of apps are being built
2. Coding in Node.js• Sync v/s Async coding (Callbacks)• Classes & Modules (CommonJS)• Node.js EventEmitters • npm & package.json
3. 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• Socket.io + Express.js (Real-time) app
4. Final demo• An sample app that uses concepts & modules talked above
Wednesday, March 28, 12
Page 8
8
About Node.js
Node.js is a non-blocking, event-driven I/O platform that is built onGoogle Chrome’s v8 engine.
- It is ~80% C/C++ & ~20% JS (APIs)- Uses CommonJS module system.- Executes JavaScript on the server- Built by Ryan Dahl - Sponsored by Joyent
Ryan Dahl(Node.js creator)
Wednesday, March 28, 12
Page 9
9
What is the biggest advantage of Node.js?
Biggest thing Node.js brings to the table (other than JS) is savings in I/O cost
Wednesday, March 28, 12
Page 10
10
The cost of I/O
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
Wednesday, March 28, 12
Page 11
11
So how does Node.js save I/O cost?
Node.js saves I/O cost by implementingNon-blocking, event driven I/O model
Wednesday, March 28, 12
Page 12
12
Event-driven, non-blocking I/O platform/server
Multi-threaded blocking server v/s
Event-driven, non-blocking server
What exactly is a event-driven, non-blocking server?
How is it different from a multi-threaded server?
Wednesday, March 28, 12
Page 13
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
DB
FS
T Thread
Wednesday, March 28, 12
Page 14
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
User1
DB
FS
T Thread
Wednesday, March 28, 12
Page 15
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
User1 i/o request
DB
FS
T Thread
Wednesday, March 28, 12
Page 16
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1User1 i/o request
DB
FS
T Thread
Wednesday, March 28, 12
Page 17
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1User1 i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 18
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1User1
User2
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 19
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1User1
User2
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 20
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
User1
User2
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 21
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 22
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 23
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 24
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 25
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 26
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 27
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4 T5
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 28
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4 T5
User4 refreshes 3 times
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 29
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4 T5
T6User4 refreshes 3 times
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 30
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4 T5
T6User4 refreshes 3 times
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 31
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4 T5
T6User4 refreshes 3 times
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 32
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4 T5
T6 T7 T8 T9User4 refreshes 3 times
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
Blocking
FS
T Thread
Wednesday, March 28, 12
Page 33
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4 T5
T6 T7 T8 T9User4 refreshes 3 times
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
BlockingI/O
FS
T Thread
Wednesday, March 28, 12
Page 34
13
Multi-threaded server - Threads are spawned for every connection
Multi threadedserver
T1
T2
T3 T4 T5
T6 T7 T8 T9User4 refreshes 3 times
User1
User2
User3Refreshes 2 times
i/o request
i/o request
DB
BlockingI/O
FS
T Because every I/o is blocking,server spawns a thread per connectionto support multiple requests
Thread
Wednesday, March 28, 12
Page 35
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
T1V8
Event loop(Libev)
Singlethreadserves all users
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 36
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 37
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 38
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 39
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8T1
V8JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 40
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 41
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 42
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 43
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 44
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 45
14
Non-blocking & Evented I/O (Node.js server)
Node.js
DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 46
14
Non-blocking & Evented I/O (Node.js server)
Node.js
User1 i/o request DB
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 47
14
Non-blocking & Evented I/O (Node.js server)
Node.js
User1 i/o request DB
Non-blockingI/O
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
delegate i/o tolibeio
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 48
14
Non-blocking & Evented I/O (Node.js server)
Node.js
User1
User2
i/o request DB
Non-blockingI/O
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
i/o request
delegate i/o tolibeio
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 49
14
Non-blocking & Evented I/O (Node.js server)
Node.js
User1
User2
i/o request DB
Non-blockingI/O
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
i/o request
delegate i/o tolibeio
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 50
14
Non-blocking & Evented I/O (Node.js server)
Node.js
User1
User2
i/o request DB
Non-blockingI/O
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
i/o request
delegate i/o tolibeio
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 51
14
Non-blocking & Evented I/O (Node.js server)
Node.js
User1
User2
User3Refreshes 2 times
i/o request DB
Non-blockingI/O
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
i/o request
delegate i/o tolibeio
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
t7
JS C/C++
Wednesday, March 28, 12
Page 52
14
Non-blocking & Evented I/O (Node.js server)
Node.js
User1
User2
User3Refreshes 2 times
i/o request DB
Non-blockingI/O
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
i/o request
delegate i/o tolibeio
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
i/o result returned 2 EL after x time
t7
JS C/C++
Wednesday, March 28, 12
Page 53
14
Non-blocking & Evented I/O (Node.js server)
Node.js
User4 refreshes 3 times
User1
User2
User3Refreshes 2 times
i/o request DB
Non-blockingI/O
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
i/o request
delegate i/o tolibeio
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
i/o result returned 2 EL after x time
t7
JS C/C++
Wednesday, March 28, 12
Page 54
14
Non-blocking & Evented I/O (Node.js server)
Node.js
User4 refreshes 3 times
User1
User2
User3Refreshes 2 times
i/o request DB
Non-blockingI/O
FS
LibioPOSIXAsync
Threadst3
t1t2
t4
t5
t6
Event loop(Libev)
Singlethreadserves all users
i/o request
delegate i/o tolibeio
T1V8
T1V8
JS Thread running your code (Single threaded)
t3
t1t2 POSIX threads doing
async I/O (multi-threaded)
i/o result returned 2 EL after x time
t7
Everything except your (JS) code is runs in parallel (by libio)
JS C/C++
Wednesday, March 28, 12
Page 55
15
Event-driven, non-blocking I/O server
Multi-threaded blocking server (Apache) v/s
Event-driven, non-blocking server (Nginx)
Real-world example of the two models?
Wednesday, March 28, 12
Page 56
16
Apache V/s Nginx: performance
Ref: http://blog.webfaction.com/a-little-holiday-present
Reqs/sec v/s concurrent connections
At ~4000 concurrent connections, - Nginx can serve ~9000 reqs/sec - Apache can serve ~3000 reqs/sec
Wednesday, March 28, 12
Page 57
17
Apache V/s Nginx: Memory usage
Ref: http://blog.webfaction.com/a-little-holiday-present
Memory v/s concurrent connections
At ~4000 concurrent connections, - Nginx uses 3MB memory- Apache uses 40MB memory
Wednesday, March 28, 12
Page 58
18
Saving I/O is great, what else is happening w/ Node.js?
Let’s look at community, libraries, buzz around Node.js
Wednesday, March 28, 12
Page 59
19
Other things going on for Node.js
2nd most popular watched on git
Wednesday, March 28, 12
Page 60
20
Other things going on for Node.js
8,000+ libraries/modules/servers
Web frameworksRoutersStatic file serversMicroframeworksFrameworksMiddlewareJSGIConnectOther middlewareOtherDatabaseMS SQL ServerPostgreSQLMySQLSQLite
OracleNoSQL and Key/ValueMongoHiveRedisCouchDBOther NoSQL implementationsMiscellaneous and multiple DBTemplatingCSS EnginesContent Management SystemsBuild and DeploymentPackage Management SystemsModule LoaderOpenSSL / Crypto / HashingSMTPTCP / IP
Multiple protocolsHTTPFTPE-mailXMPPOther networkingRPCWeb Sockets & AjaxMessage QueuesClass systemsTesting / Spec FrameworksWrappersParsersJSONXML
Command Line Option ParsersParser GeneratorsOther ParsersDebugging / Console UtilitiesCompressionGraphicsSoundPayment GatewaysAPI clientsControl flow / Async goodiesI18n and L10n modulesBoilerplatesContinuous Integration ToolsDDD, CQRS, EventSourcingDesktop application relatedJavaScript threadsOther
High-level library categories
https://github.com/joyent/node/wiki/modules
Wednesday, March 28, 12
Page 61
21
Other things going on for Node.js
Node in Production!• LinkedIn, Yahoo!, Microsoft, 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• Ldapjs – - Node.js implementation of LDAP server itself• SMTP – Node.js implementation of SMTP server itself• XMPP, SSH, RPC, many more.
Wednesday, March 28, 12
Page 62
22
Agenda – part 2
1. About Node.js• Internal working of Node.js• Buzz around Node.js• Who is using it• What kind of apps are being built
2. Coding in Node.js• Sync v/s Async coding (Callbacks)• Classes & Modules (CommonJS)• Node.js EventEmitters• npm & package.json
3. Node.js & Cloud Foundry• Hello World app in Cloud Foundry• Using Sticky Sessions• CloudFoundry Module & connecting to Redis, MongoDB etc • Express.js (RESTful) app• Socket.io + Express.js (Real-time) app
4. Demo• An sample app that uses concepts & modules talked above
Wednesday, March 28, 12
Page 63
23
Let’s look at the code..
Synchronous codev/s
Asynchronous Code
How does async code differ from sync(regular) code?
Wednesday, March 28, 12
Page 64
24
Callbacks – Control flow
//Synchronous & blocking codefunction getItemNameById(id) { //blocks or waits for DB return db.get(id); //step 2}
var name = getItemNameById(100); //step 1
//print name in step 3console.log(name); //step 3
//Async & non-blocking code function getItemNameById(id, callback) { db.get(id, callback); //step 2 //nothing is returned here}
//step 3Some internal function calls the callback w/ result
//You create a callback helper function function displayHelperCallback(name) { console.log(name); //step 4}
//pass callback function to consume the resultgetItemNameById(100, displayHelperCallback); //step 1
Things to note:1. Async code doesn’t directly ‘return’
anything2. Instead, it takes a function(callback)
& calls that function when result becomes available
Use case: Let’s say we have an item’s id and want to get its name from DB and print it
Wednesday, March 28, 12
Page 65
25
Callbacks – Control flow (detailed version in Node.js)
//INTERNALS OF A DB LIBRARY (HIGH LEVEL)
function db() { this.dbConnection = net.connection(); // connects to DB}db.prototype.get = function(id, callback) { var self = this; //step 3 this.dbConnection.write(id, function(result) { //step 4 self. receiveFromDB(result, callback);//step 101 }); }
db.prototype.receiveFromDB = function(result, callback) { callback(result); //Execute callback step step 102 }
//YOUR APPvar db = require(‘db’);function getItemNameById(id, callback) { db.get(id, callback); //step 2}
//You create a callback helper function function displayHelperCallback(name) { console.log(name); //step 103}
//pass callback function to consume the resultgetItemNameById(100, displayHelperCallback); //step 1
//step 5V8 is free to run other functions in the event-loop.
//step 5, step 6 ..step 100Say v8 notices 95 other things to do (in the event loop), it starts executing them one by one.
At some point b/w step 3 and step 100, returns result & asks to run dbConnection.write’s callback.
This event goes to the back of the queue as step 101
Step 5
Wednesday, March 28, 12
Page 66
26
Node.js
Classes & CommonJS module
How can I better organize my code?
Wednesday, March 28, 12
Page 67
27
JavaScript Classes (Prototypical inheritance)
//JavaScript prototypical inheritance
//Super Classfunction Automobile(license, model) { this.license = license; this.model = model;}
Automobile.prototype.getModel = function() { return model;}
//Sub classfunction Car(license, model) { Automobile.call(this, license, model);}
Car.prototype = new Automobile();Car.prototype.constructor = Car;
Node.js also provides handy util.inherits function to inherit.- This also provides ‘subclass.super_’ to access
super class’ functions
var require(‘util’); //import util module
//Super Classfunction Automobile(license, model) { this.license = license; this.model = model;}
Automobile.prototype.getModel = function() { return model;}
//Sub classfunction Car(license, model) { Automobile.call(this, license, model);}
util.inherits(Car, Automobile);
Wednesday, March 28, 12
Page 68
28
CommonJS modules
//Automobile.js filefunction Automobile(license, model) { this.license = license; this.model = model;}
Automobile.prototype.getModel = function() { return model;}exports.Automobile = Automobile;
//Car.js filevar util = require('util');var module = require('./Automobile');var Automobile = module.Automobile;
function Car(license, model) { Automobile.call(this, license, model);}
util.inherits(Car, Automobile);
console.log(new Car("1232", "BMW").model); //prints BMW
Things to note:1. Allows keeping JS code in separate
files
2. Use “exports.<name>” to export something
1. Use require(‘path/to/module’) to import it
2. use require(‘module’).<name> to access things inside module
Wednesday, March 28, 12
Page 69
29
CommonJS modules: Exporting multiple things
//myModule.js fileexports.myFunction = function () { return ‘hi there’;}exports.myArray = [‘foo’, ‘bar’];
exports.myVariable = ‘I’m a variable’;
//app.js filevar myModule = require('./myModule');
console.log(myModule.myFunction()); //prints ‘’hi there’console.log(myModule.myArray[1]); //prints ‘bar’console.log(myModule.myVariable); //prints I’m a variable’
Things to note:1. You can directly export function,
arrays, variables
2. You can export multiple things from one file using ‘exports’
Wednesday, March 28, 12
Page 70
30
CommonJS modules: ‘exports’ v/s ‘module.exports’
//myModule.js filemodule.exports = function () { return ‘hi there’;}
//app.js filevar myFunction = require('./myModule');
console.log(myModule.myFunction()); //prints ‘’hi there’
Things to note:If you want to export only one thing.. so that it can be used directly by the recepient, you can use..module.exports = <something>;
Warning:If you use both module.exports and exports.bla, exports.bla will NOT be exported(ignored)
Wednesday, March 28, 12
Page 71
31
Node.js EventEmitter (A utility class that allows emitting events)
Node.js EventEmitter
EventEmitter class implements Observer pattern and provides on and emit APIs • It is used when creating(not using) an async library.
Wednesday, March 28, 12
Page 72
32
Events – Node.js EventEmitter (A node.js utility class that allows emitting events)
//Simplified EventEmitter (Observer pattern)
function EventEmitter() { //store events and callbacks like {event1: [callback1, callback2] , event2 : [cb3, cb4]…} this.eventNameAndCallbackList = {}; }
//Allow others to add a callback(function) for a event name(string)EventEmitter.prototype.on = function(eventName, callback) { //add eventName and callback to eventNameAndCallbackList };
//When an event is emitted, call each callbacks in a loopEventEmitter.prototype.emit = function(eventName) { for(var i =0; i < currentCallbacks.length ; i++) { currentCallbacks[i](); //call each callback } };
exports.EventEmitter = EventEmitter;
Wednesday, March 28, 12
Page 73
33
Events – Node.js EventEmitter (continued)
//myIOModule.jsvar util = require('util');var events = require('events');
//myIOClass is a subclass of events.EventEmitter classvar MyIOClass = function () { events.EventEmitter.call(this);};util.inherits(MyIOClass, events.EventEmitter);
MyIOClass.prototype.readFromDB = function(query){ // <--reads data code here --> this.emit('data', "some data");}exports.MyIOClass = MyIOClass; //export the class
Say you are writing an I/O library & writing readFromDB function but don’t know how to handleasync DB result.
You can solve it by..1. Inheriting your class from EventEmitter 2. Then you can use its ‘emit’ function to an event when data arrives (asynchronously)3. You ask people who’ll be using your library to implement ‘on’ function
Wednesday, March 28, 12
Page 74
34
Events – Node.js EventEmitter (continued)
//app.jsvar myIOModule = require('./myIOModule');
var myIOClass = new myIOModule.MyIOClass();myIOClass.on('data', function (data) { console.log(data);});
myIOClass.readFromDB('select * from users');
Say you are an end-user trying to use DB library to read result from DB..
1. You’ll have to implement ‘on’ function for the given event name (‘data’) and set a callback2. DB libraries internal function will call your callback when the result comes back
Wednesday, March 28, 12
Page 75
35
Events – A library can emit multiple events
I/O libraries usually emit multiple events.. connected, disconnected, error, ready, data, result etc.
//So you can listen to all of them..function myFunction() { db.on(‘error’, function(e) { console.error(e); }); db.on(‘connect’, function() { //db is connected db.query(user); }); db.on(‘disconnect’, function(){ console.log(‘db disconnected’); });
db.connect(‘127.0.0.1’, ‘100’);}
Wednesday, March 28, 12
Page 76
36
Events – Error/Exception handling
//Say there was an exception trying to connect to db. Function () { try { db.connect(‘127.0.0.1’, ‘4000’); // failed to connect; connectionException } catch (e) { console.error(e); } }
Above try/catch won’t handle it because the act of connection itself is an i/o
//Say there was an exception trying to connect to db. Function () { //Typically I/O libraries triggers ‘error’ event. We’ll need to listen to that event db.on(‘error’, function(e) { console.error(e); });
db.connect(‘127.0.0.1’, ‘100’); // failed to connect; connectionException}
Wednesday, March 28, 12
Page 77
37
Installing external modules – npm (Node Package Manager)
Use npm (Node Package Manager) to install modulesnpm install <moduleName>e.x. npm install express
Modules are copied into ./node_modules folder /myapp/myapp/node_modules/express
//Instead keep ALL dependencies in package.json file in root of your app and run:npm install
//package.json{ "name": ”MyApp", "description": ”My awesome twitter app", "version": "2.5.8", "author": ”Raja<[email protected] >", "dependencies": { "express": “2.3.4”, "mime": "", "connect-redis": ">= 0.0.1" }}
Things to note:1. npm = Node Package Manager2. It is a CLI to install modules from3. Using package.json is preferred
1. Need to be careful about versions
2. You need to have all the modules pre-installed (i.e. npm install) before uploading your app to Cloud Foundry
Wednesday, March 28, 12
Page 78
38
Installing external modules - npm & package.json
Use npm (Node Package Manager) to install modulesnpm install <moduleName>e.x. npm install express
Modules are copied into ./node_modules folder /myapp/myapp/node_modules/express
//Instead keep ALL dependencies in package.json file in root of your app and run:npm install
//package.json{ "name": ”MyApp", "description": ”My awesome twitter app", "version": "2.5.8", "author": ”Raja<[email protected] >", "dependencies": { "express": “2.3.4”, "mime": "", "connect-redis": ">= 0.0.1" }}
Things to note:1. npm = Node Package Manager2. It is a CLI to install modules from3. Using package.json is preferred
1. Need to be careful about versions
2. You need to have all the modules pre-installed (i.e. npm install) before uploading your app to Cloud Foundry
Wednesday, March 28, 12
Page 79
39
Agenda – part 3
1. About Node.js• Internal working of Node.js• Buzz around Node.js• Who is using it• What kind of apps are being built
2. Coding in Node.js• Sync v/s Async coding (Callbacks)• Classes & Modules (CommonJS)• Node.js EventEmitters• npm & package.json
3. Node.js & Cloud Foundry (w/ demo)• Cloud Foundry – Open PaaS• Hello World app in Cloud Foundry• Scaling - Using Sticky Sessions• CloudFoundry Module & connecting to Redis, MongoDB etc • Express.js (RESTful) app• Socket.io + Express.js (Real-time) app
4. Demo• An sample app that uses concepts & modules talked above
Wednesday, March 28, 12
Page 80
40
Cloud Foundry
Cloud Foundry – Open Platform as a Service
Wednesday, March 28, 12
Page 81
What is Cloud Computing?
41
Cloud According to my daughter Eliette (10 years old)
Wednesday, March 28, 12
Page 82
Cloud Stack - Classic Pyramid
42
Platform As A Service
Infrastructure As A Service
SoftwareAs A Service
Wednesday, March 28, 12
Page 83
Cloud Stack - By Value
43
InfrastructureAs A Service
SoftwareAs A Service
Platform As A Service
Wednesday, March 28, 12
Page 84
Agility as a survival skill
§ Consumer software is becoming like fashion•Phone apps, social apps, short lifetime, fast lifecycles
•Ab testing
§ Clay shirky situational apps
§ Kent Beck, Usenix 2011 Talk, “Software G-Forces”change in software process when frequency grows
§ Cloud Platforms enables an Agile culture, driver for innovation•Scalability is built in the platforms
•Can iterate faster
•Focus on design
§ Cloud Platforms lets developers focus on driving business value
44
Wednesday, March 28, 12
Page 85
Platforms
§ Raise the Unit of currency to be application & services instead of infrastructure
§ Google App Engine, Cloud Foundry, Joyent, Heroku, Stax (Cloudbees), Amazon elastic beanstalk, Microsoft Azure, AppFog
§ Single or a few languages, services
§ Start of Multi language Polyglot platforms
§ Enabler for Agile Developers -> Create Business value faster
§ Lack of standards: risk, vendor lock-in
§ Enterprise needs:•Control, customizability
•Private/Hybrid Cloud
•Avoid lock-in
45
Wednesday, March 28, 12
Page 86
Main Risk: Lock-In
46
Welcome to the hotel californiaSuch a lovely placeSuch a lovely facePlenty of room at the hotel californiaAny time of year, you can find it here
Last thing I remember, I wasRunning for the doorI had to find the passage backTo the place I was before’relax,’ said the night man,We are programmed to receive.You can checkout any time you like,But you can never leave!
Wednesday, March 28, 12
Page 87
Cloud Foundry open PaaS - Choice of frameworks
Wednesday, March 28, 12
Page 88
Cloud Foundry open PaaS - Choice of frameworks
OSS community
Wednesday, March 28, 12
Page 89
Applica'on Service Interface
Data Services
Other Services
Msg Services
Cloud Foundry open PaaS - Choice of application services
Wednesday, March 28, 12
Page 90
Applica'on Service Interface
Data Services
Other Services
Msg Services
Cloud Foundry open PaaS - Choice of application services
vFabric Postgres
vFabric RabbitMQTM
Additional partners services …
Wednesday, March 28, 12
Page 91
Applica'on Service Interface
Data Services
Other Services
Msg Services
Cloud Foundry open PaaS - Choice of clouds
Private Clouds
PublicClouds
MicroClouds
.COM
PartnersClo
ud Provide
r Interface
Wednesday, March 28, 12
Page 92
Applica'on Service Interface
Data Services
Other Services
Msg Services
Cloud Foundry open PaaS - Choice of clouds
Private Clouds
PublicClouds
MicroClouds
.COM
PartnersClo
ud Provide
r Interface
Avoid
Lock-in
Wednesday, March 28, 12
Page 93
Applica'on Service Interface
Data Services
Other Services
Msg Services
Cloud Foundry open PaaS - Choice of clouds
Private Clouds
PublicClouds
MicroClouds
.COM
PartnersClo
ud Provide
r Interface
Avoid
Lock-in
OSS community
Wednesday, March 28, 12
Page 94
Open Source Advantage: it gets better faster!
§ http://code.google.com/p/googleappengine/issues/detail?id=13
50
• https://github.com/cloudfoundry/vcap/pull/25
Wednesday, March 28, 12
Page 95
51
Cloud Foundry open Platform as a Service
The PaaS of choice for the Cloud era
Simple• Let’s developers focus on their code and not wiring middleware
Open• Avoid lock-in to specific cloud, frameworks or service• Completely open source from day one
Flexible and Scalable• Self service, deploy and scale your applications in seconds• Extensible architecture to “digest” future cloud innovation
Wednesday, March 28, 12
Page 96
Cloud for Developers: the true path is PaaS!
52
Wednesday, March 28, 12
Page 97
53
Node.js on Cloud foundry (Demos and Examples)
Wednesday, March 28, 12
Page 98
54
Hello World App on Cloud Foundry
//Simple http server on localhostvar http = require('http');http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');}).listen(3000, '127.0.0.1');console.log('Server running at 127.0.0.1:3000');
//Simple http server on Cloud Foundryvar http = require('http');var host = process.env.VCAP_APP_HOST || ‘localhost’;var port = process.env.VCAP_APP_PORT || ‘3000’;http.createServer(function (req, res) { res.writeHead(200, {'Content-Type': 'text/plain'}); res.end('Hello World\n');}).listen(port, host);console.log('Server running at ’ + host + “:” + port);
Things to note:1. Cloud Foundry will provide host and port info in process.env
variable2. You can use https to access your app (up to nginx; http w/in CF)3. Save your app as app.js or server.js to automatically use that as
main node.js
Wednesday, March 28, 12
Page 99
55
Hello World App using ExpressJS on Cloud Foundry
var host = process.env.VCAP_APP_HOST || 'localhost';var port = process.env.VCAP_APP_PORT || '3000';
var express = require('express');var app = express.createServer();
app.configure('development', function() { app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));});
app.configure('production', function() { app.use(express.errorHandler());});
app.get('/user/', function(req, res){ throw new Error(“call me w/ a user id");});
app.get('/user/:id', function(req, res){ res.send('user ' + req.params.id);});app.listen(port, host);
Things to note:1. ExpressJS is Ruby Sinatra
inspired web framework2. It provides support for:
1. Dev/Prod Configurations2. Routes3. Templating4. Sessions5. Many, many other features
Wednesday, March 28, 12
Page 100
56
Hello World App w/ built-in node.js cluster ( > 0.6)
var cluster = require('cluster');var http = require('http');var numCPUs = require('os').cpus().length;var port = process.env.VCAP_APP_PORT || 3000;var host = process.env.VCAP_APP_HOST || 'localhost';
if (cluster.isMaster) { //fork workersfor (var i = 0; i < numCPUs; i++) { cluster.fork(); } cluster.on('death', function(worker) { console.log('worker ' + worker.pid + ' died'); });} else { // Each worker processes have a http server. http.Server(function(req, res) { res.writeHead(200); res.end(host + ":" + port); }).listen(port, host); console.log("listening on.. " + host + ":" + port);}
Things to note:1. Node.js started to support Windows servers
• Supposedly runs faster on Windows J
1. If you are using cluster or need node 0.6 or greater, use node06 runtime
i.e. vmc push myapp --runtime=node06
2. This example spawns 4 node processes per instance!
• Watch out, it might exceeds default 64MB memory.
3. You may have to implement Redis based-session store
Wednesday, March 28, 12
Page 101
57
Agenda – part 3
1. About Node.js• Internal working of Node.js• Buzz around Node.js• Who is using it• What kind of apps are being built
2. Coding in Node.js• Sync v/s Async coding (Callbacks)• Classes & Modules (CommonJS)• Node.js EventEmitters• npm & package.json
3. Node.js & Cloud Foundry (w/ demo)• Cloud Foundry – Open PaaS• Hello World app in Cloud Foundry• Scaling - Using Sticky Sessions• CloudFoundry Module & connecting to Redis, MongoDB etc • Express.js (RESTful) app• Socket.io + Express.js (Real-time) app
4. Demo• An sample app that uses concepts & modules talked above
Wednesday, March 28, 12
Page 102
Cloud Foundry Resources
58
Primary Site : cloudfoundry.com
Open Source Site : cloudfoundry.org
Twitter : @cloudfoundry , hash tag #cfoundry
Blog : blog.cloudfoundry.com
FB : facebook.com/cloudfoundry
Support : support.cloudfoundry.com• Documentation
• Knowledge Base
• Q & A / Forums
Wednesday, March 28, 12
Page 103
Questions
59
?Wednesday, March 28, 12
Page 104
Thank You!@CloudFoundry
@chanezon
Website : www.cloudfoundry.comBlog : blog.cloudfoundry.comForum : support.cloudfoundry.com
t
t
Wednesday, March 28, 12
Page 105
Books / Articles
§ Nick Carr, The Big Switch
§ Eric Raymond, The Art of Unix Programming
§ Weinberg, Psychology of Computer Programming
§ Wes python book
§ Mark html5 book
§ Kent Beck XP
§ Hunt, Thomas, The Pragmatic Programmer
§ Ade Oshineye, Apprenticeship Patterns
§ Matt Cutt's Ignite Talk IO 2011, Trying different things
§ Josh Bloch talk about api design
§ Larry and Sergey, Anatomy of a Search Engine
§ Rob Pike, The Practice of Programming
61
Wednesday, March 28, 12
Page 106
Papers / Talks
§ Simon Wardley, Oscon 09 “Cloud - Why IT Matters”
§ Tim O’Reilly article on internet os
§ Peter Deutsch’s 8 Fallacies of Distributed Computing
§ Brewer’s CAP Theorem
§ Gregor Hohpe’s Starbucks Does Not Use Two-Phase Commit
§ Herb Sutter, Welcome to the Junglehttp://herbsutter.com/welcome-to-the-jungle/
§ Stuff I tag http://www.delicious.com/chanezon/
§ More specifically http://www.delicious.com/chanezon/cloudfoundry
§ My previous Talks http://www.slideshare.net/chanezon
§ My list of favorite books http://www.chanezon.com/pat/soft_books.html
62
Wednesday, March 28, 12
Page 107
Acknowledgement
§ Drawings from my daughters Eliette
§ Slides from Dave McCrory, Derek Collison, Russell Acton, Alexandre Vasseur, Raja Rao
§ Chinese poetry from Wang Wei, Zhu Yunming, Jiao Dao, Li Bai from “The Anchor book of Chinese Poetry”, Tony Barnstone and Chou Ping
63
Wednesday, March 28, 12