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
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)
3
Wednesday, March 28, 12
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)
4
Wednesday, March 28, 12
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
mountain
but who knows where? The clouds are deep.”
Jiao Dao (778-841)
5
Wednesday, March 28, 12
6. Looking for the Hermit and Not Finding Him
Lu mountain stands beside the Southern Dipper
In clouds reaching silken like a nine-panelled screen
With its shadows in a crystal lake deepening the green water.
The Golden Gate opens into two mountain ranges.
Li Bai (701-762)
6
Wednesday, March 28, 12
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
7
Wednesday, March 28, 12
8. About Node.js
Node.js is a non-blocking, event-driven I/O platform that is built on
Google 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)
8
Wednesday, March 28, 12
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
9
Wednesday, March 28, 12
10. The cost of I/O
http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/
10
Wednesday, March 28, 12
11. So how does Node.js save I/O cost?
Node.js saves I/O cost by implementing
Non-blocking, event driven I/O model
11
Wednesday, March 28, 12
12. Event-driven, non-blocking I/O platform/server
What exactly is a event-driven, non-blocking server?
How is it different from a multi-threaded server?
Multi-threaded blocking server
v/s
Event-driven, non-blocking server
12
Wednesday, March 28, 12
13. Multi-threaded server - Threads are spawned for every connection
DB
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
14. Multi-threaded server - Threads are spawned for every connection
User1
DB
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
15. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
DB
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
16. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
17. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
Blocking
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
18. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2
Blocking
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
19. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request
Blocking
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
20. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
21. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3
Refreshes 2 times
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
22. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3
Refreshes 2 times
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
23. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3
Refreshes 2 times
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
24. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3
Refreshes 2 times
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
25. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3 T4
Refreshes 2 times
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
26. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3 T4
Refreshes 2 times
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
27. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3 T4 T5
Refreshes 2 times
FS
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
28. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3 T4 T5
Refreshes 2 times
User4 FS
refreshes 3 times
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
29. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3 T4 T5
Refreshes 2 times
User4 T6 FS
refreshes 3 times
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
30. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3 T4 T5
Refreshes 2 times
User4 T6 FS
refreshes 3 times
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
31. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3 T4 T5
Refreshes 2 times
User4 T6 FS
refreshes 3 times
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
32. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
User3 T3 T4 T5
Refreshes 2 times
T6 T7 T8 FS
User4 T9
refreshes 3 times
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
33. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
I/O
User3 T3 T4 T5
Refreshes 2 times
T6 T7 T8 FS
User4 T9
refreshes 3 times
Multi threaded
server
T Thread
13
Wednesday, March 28, 12
34. Multi-threaded server - Threads are spawned for every connection
User1 i/o request
T1
DB
User2 i/o request T2
Blocking
I/O
User3 T3 T4 T5
Refreshes 2 times
T6 T7 T8 FS
User4 T9
refreshes 3 times
Multi threaded
server
T Thread
Because every I/o is blocking,
server spawns a thread per connection
to support multiple requests
13
Wednesday, March 28, 12
35. Non-blocking & Evented I/O (Node.js server)
JS C/C++
T1
DB
V8
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
36. Non-blocking & Evented I/O (Node.js server)
JS C/C++
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
37. Non-blocking & Evented I/O (Node.js server)
JS C/C++
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
T1 t7 FS
V8
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
38. Non-blocking & Evented I/O (Node.js server)
JS C/C++
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
39. Non-blocking & Evented I/O (Node.js server)
JS C/C++
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1
T1 JS Thread running V8
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
40. Non-blocking & Evented I/O (Node.js server)
JS C/C++
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
41. Non-blocking & Evented I/O (Node.js server)
JS C/C++
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves T1 t6
all users V8 t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
42. Non-blocking & Evented I/O (Node.js server)
JS C/C++
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
43. Non-blocking & Evented I/O (Node.js server)
JS C/C++
DB
T1
Event loop V8
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
44. Non-blocking & Evented I/O (Node.js server)
JS C/C++
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
45. Non-blocking & Evented I/O (Node.js server)
JS C/C++
T1
V8
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
46. Non-blocking & Evented I/O (Node.js server)
JS C/C++
T1
User1 i/o request V8
DB
Event loop
(Libev)
t2
t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
47. Non-blocking & Evented I/O (Node.js server)
JS C/C++
T1
User1 i/o request V8
DB
Event loop
(Libev)
delegate i/o to Non-blocking
t2 I/O
libeio t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
48. Non-blocking & Evented I/O (Node.js server)
JS C/C++
T1
User1 i/o request V8
DB
Event loop
User2 i/o request (Libev)
delegate i/o to Non-blocking
t2 I/O
libeio t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
49. Non-blocking & Evented I/O (Node.js server)
JS C/C++
User1 i/o request
DB
Event loop
User2 i/o request (Libev)
delegate i/o to Non-blocking
t2 I/O
libeio t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
50. Non-blocking & Evented I/O (Node.js server)
JS C/C++
User1 i/o request
DB
Event loop
User2 i/o request (Libev)
T1 delegate i/o to Non-blocking
t2 I/O
V8 libeio t1Libio
POSIX
Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
51. Non-blocking & Evented I/O (Node.js server)
JS C/C++
User1 i/o request
DB
Event loop
User2 i/o request (Libev)
T1 delegate i/o to Non-blocking
t2 I/O
V8 libeio t1Libio
User3 POSIX
Refreshes 2 times Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
52. Non-blocking & Evented I/O (Node.js server)
JS C/C++
User1 i/o request
DB
Event loop
User2 i/o request (Libev)
T1 delegate i/o to Non-blocking
t2 I/O
V8 libeio t1Libio
User3 POSIX
Refreshes 2 times Single Asynct4
thread t3
Threads
serves t6
all users t5
t7 FS
i/o result returned
2 EL after x time
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
53. Non-blocking & Evented I/O (Node.js server)
JS C/C++
User1 i/o request
DB
Event loop
User2 i/o request (Libev)
T1 delegate i/o to Non-blocking
t2 I/O
V8 libeio t1Libio
User3 POSIX
Refreshes 2 times Single Asynct4
thread t3
Threads
serves t6
all users t5
User4
refreshes 3 times
t7 FS
i/o result returned
2 EL after x time
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3
14
Wednesday, March 28, 12
54. Non-blocking & Evented I/O (Node.js server)
JS C/C++
User1 i/o request
DB
Event loop
User2 i/o request (Libev)
T1 delegate i/o to Non-blocking
t2 I/O
V8 libeio t1Libio
User3 POSIX
Refreshes 2 times Single Asynct4
thread t3
Threads
serves t6
all users t5
User4
refreshes 3 times
t7 FS
i/o result returned
2 EL after x time
T1 JS Thread running
V8 your code (Single threaded)
Node.js
t1 POSIX threads doing
t2
async I/O (multi-threaded)
t3 Everything except your (JS) code is runs in parallel (by libio)
14
Wednesday, March 28, 12
55. Event-driven, non-blocking I/O server
Real-world example of the two models?
Multi-threaded blocking server (Apache)
v/s
Event-driven, non-blocking server (Nginx)
15
Wednesday, March 28, 12
56. Apache V/s Nginx: performance
Reqs/sec v/s concurrent connections
At ~4000 concurrent connections,
- Nginx can serve ~9000 reqs/sec
- Apache can serve ~3000 reqs/sec
Ref: http://blog.webfaction.com/a-little-holiday-present
16
Wednesday, March 28, 12
58. Saving I/O is great, what else is happening w/ Node.js?
Let’s look at community, libraries, buzz around Node.js
18
Wednesday, March 28, 12
59. Other things going on for Node.js
2nd most popular watched on git
19
Wednesday, March 28, 12
60. Other things going on for Node.js
8,000+ libraries/modules/servers
High-level library categories
Web frameworks Oracle Multiple protocols Command Line Option Parsers
NoSQL and Key/Value HTTP Parser Generators
Routers
Mongo FTP Other Parsers
Static file servers Debugging / Console Utilities
Hive E-mail
Microframeworks Redis XMPP Compression
Frameworks Graphics
CouchDB Other networking Sound
Middleware Other NoSQL implementations RPC Payment Gateways
JSGI Miscellaneous and multiple DB Web Sockets & Ajax API clients
Connect Templating Message Queues Control flow / Async goodies
Other middleware CSS Engines Class systems I18n and L10n modules
Content Management Systems Testing / Spec Frameworks Boilerplates
Other Build and Deployment Wrappers Continuous Integration Tools
Database Package Management Systems Parsers DDD, CQRS, EventSourcing
MS SQL Server Module Loader JSON Desktop application related
OpenSSL / Crypto / Hashing XML JavaScript threads
PostgreSQL Other
MySQL SMTP
SQLite TCP / IP
https://github.com/joyent/node/wiki/modules
20
Wednesday, March 28, 12
61. 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.
21
Wednesday, March 28, 12
62. 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
22
Wednesday, March 28, 12
63. Let’s look at the code..
How does async code differ from sync(regular) code?
Synchronous code
v/s
Asynchronous Code
23
Wednesday, March 28, 12
64. Callbacks – Control flow
Use case: Let’s say we have an item’s id and want to get its name from DB and print it
//Synchronous & blocking code //Async & non-blocking code
function getItemNameById(id) { function getItemNameById(id, callback) {
//blocks or waits for DB db.get(id, callback); //step 2
return db.get(id); //step 2 //nothing is returned here
} }
var name = getItemNameById(100); //step 1 //step 3
Some internal function calls the callback w/ result
//print name in step 3
console.log(name); //step 3 //You create a callback helper function
function displayHelperCallback(name) {
console.log(name); //step 4
}
Things to note: //pass callback function to consume the result
getItemNameById(100, displayHelperCallback); //step 1
1. Async code doesn’t directly ‘return’
anything
2. Instead, it takes a function(callback)
& calls that function when result
becomes available
24
Wednesday, March 28, 12
65. Callbacks – Control flow (detailed version in Node.js)
//YOUR APP
var 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 result //step 5
getItemNameById(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 100
function db() { Say v8 notices 95 other things to do (in the
this.dbConnection = net.connection(); // connects to DB event loop), it starts executing them one by
} one.
db.prototype.get = function(id, callback) {
var self = this; //step 3
this.dbConnection.write(id, function(result) { //step 4 At some point b/w step 3 and step 100,
Step 5 returns result & asks to run
self. receiveFromDB(result, callback);//step 101
}); dbConnection.write’s callback.
}
This event goes to the back of the queue as
db.prototype.receiveFromDB = function(result, callback) { step 101
callback(result); //Execute callback step step 102
}
25
Wednesday, March 28, 12
66. Node.js
How can I better organize my code?
Classes & CommonJS module
26
Wednesday, March 28, 12
67. JavaScript Classes (Prototypical inheritance)
//JavaScript prototypical inheritance Node.js also provides handy util.inherits function to
inherit.
//Super Class - This also provides ‘subclass.super_’ to access
function Automobile(license, model) { super class’ functions
this.license = license;
this.model = model; var require(‘util’); //import util module
}
//Super Class
Automobile.prototype.getModel = function() { function Automobile(license, model) {
return model; this.license = license;
} this.model = model;
}
//Sub class
function Car(license, model) { Automobile.prototype.getModel = function() {
Automobile.call(this, license, model); return model;
} }
Car.prototype = new Automobile(); //Sub class
Car.prototype.constructor = Car; function Car(license, model) {
Automobile.call(this, license, model);
}
util.inherits(Car, Automobile);
27
Wednesday, March 28, 12
68. CommonJS modules
//Automobile.js file Things to note:
function Automobile(license, model) { 1. Allows keeping JS code in separate
this.license = license; files
this.model = model;
}
2. Use “exports.<name>” to export
Automobile.prototype.getModel = function() { something
return model;
} 1. Use require(‘path/to/module’) to
exports.Automobile = Automobile; import it
2. use require(‘module’).<name> to
access things inside module
//Car.js file
var 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
28
Wednesday, March 28, 12
69. 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 things from
exports.myVariable = ‘I’m a variable’; one file using ‘exports’
//app.js file
var 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’
29
Wednesday, March 28, 12
70. CommonJS modules: ‘exports’ v/s ‘module.exports’
//myModule.js file Things to note:
module.exports = function () { If you want to export only one
return ‘hi there’; thing.. so that it can be used
} directly by the recepient, you can
use..
module.exports = <something>;
Warning:
If you use both module.exports
//app.js file and exports.bla, exports.bla will
var myFunction = require('./myModule'); NOT be exported(ignored)
console.log(myModule.myFunction()); //prints ‘’hi there’
30
Wednesday, March 28, 12
71. Node.js EventEmitter (A utility class that allows emitting events)
EventEmitter class implements Observer pattern and provides on and emit
APIs
• It is used when creating(not using) an async library.
Node.js EventEmitter
31
Wednesday, March 28, 12
72. 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 loop
EventEmitter.prototype.emit = function(eventName) {
for(var i =0; i < currentCallbacks.length ; i++) {
currentCallbacks[i](); //call each callback
}
};
exports.EventEmitter = EventEmitter;
32
Wednesday, March 28, 12
73. Events – Node.js EventEmitter (continued)
Say you are writing an I/O library & writing readFromDB function but don’t know how to handle
async 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
//myIOModule.js
var util = require('util');
var events = require('events');
//myIOClass is a subclass of events.EventEmitter class
var 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
33
Wednesday, March 28, 12
74. Events – Node.js EventEmitter (continued)
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 callback
2. DB libraries internal function will call your callback when the result comes back
//app.js
var myIOModule = require('./myIOModule');
var myIOClass = new myIOModule.MyIOClass();
myIOClass.on('data', function (data) {
console.log(data);
});
myIOClass.readFromDB('select * from users');
34
Wednesday, March 28, 12
75. 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’);
}
35
Wednesday, March 28, 12
76. 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
}
36
Wednesday, March 28, 12
77. Installing external modules – npm (Node Package Manager)
Use npm (Node Package Manager) to install modules Things to note:
npm install <moduleName> 1. npm = Node Package Manager
e.x. npm install express 2. It is a CLI to install modules from
3. Using package.json is preferred
Modules are copied into ./node_modules folder
/myapp
/myapp/node_modules/express 1. Need to be careful about versions
2. You need to have all the modules
pre-installed (i.e. npm install)
//Instead keep ALL dependencies in package.json file in
root of your app and run: before uploading your app to
npm install Cloud Foundry
//package.json
{
"name": ”MyApp",
"description": ”My awesome twitter app",
"version": "2.5.8",
"author": ”Raja<rajar@vmware.com>",
"dependencies": {
"express": “2.3.4”,
"mime": "",
"connect-redis": ">= 0.0.1"
}
}
37
Wednesday, March 28, 12
78. Installing external modules - npm & package.json
Use npm (Node Package Manager) to install modules Things to note:
npm install <moduleName> 1. npm = Node Package Manager
e.x. npm install express 2. It is a CLI to install modules from
3. Using package.json is preferred
Modules are copied into ./node_modules folder
/myapp
/myapp/node_modules/express 1. Need to be careful about versions
2. You need to have all the modules
pre-installed (i.e. npm install)
//Instead keep ALL dependencies in package.json file in
root of your app and run: before uploading your app to
npm install Cloud Foundry
//package.json
{
"name": ”MyApp",
"description": ”My awesome twitter app",
"version": "2.5.8",
"author": ”Raja<rajar@vmware.com>",
"dependencies": {
"express": “2.3.4”,
"mime": "",
"connect-redis": ">= 0.0.1"
}
}
38
Wednesday, March 28, 12
79. 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
39
Wednesday, March 28, 12
80. Cloud Foundry
Cloud Foundry – Open Platform as a Service
40
Wednesday, March 28, 12
81. What is Cloud Computing?
Cloud According to my daughter Eliette (10 years old)
41
Wednesday, March 28, 12
82. Cloud Stack - Classic Pyramid
Software
As A Service
Platform As A Service
Infrastructure As A Service
42
Wednesday, March 28, 12
83. Cloud Stack - By Value
Software
As A Service
Platform As A Service
Infrastructure
As A Service
43
Wednesday, March 28, 12
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
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
86. Main Risk: Lock-In
Welcome to the hotel california
Such a lovely place
Such a lovely face
Plenty of room at the hotel california
Any time of year, you can find it here
Last thing I remember, I was
Running for the door
I had to find the passage back
To 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!
46
Wednesday, March 28, 12
88. Cloud Foundry open PaaS - Choice of frameworks
OSS community
Wednesday, March 28, 12
89. Cloud Foundry open PaaS - Choice of application services
Ap
p
Data
lica
Services
'o
n
S
erv
Msg
ice
Services
In
ter
fac
Other
e
Services
Wednesday, March 28, 12
90. Cloud Foundry open PaaS - Choice of application services
vFabric
Postgres
Ap
p
Data
lica
Services
'o
n
S
vFabric
erv
RabbitMQTM Msg
ice
Services
In
ter
fac
Other
e
Services
Additional partners
services …
Wednesday, March 28, 12
91. Cloud Foundry open PaaS - Choice of clouds
Ap
e
fac
Private
p
Data
lica
Services
ter
Clouds
'o
r
In
n
S
ide
erv
Public
ov
Msg
ice
Services
Pr
Clouds Partners
In
ud
ter
Cl o
.COM
fac
Other Micro
e
Services
Clouds
Wednesday, March 28, 12
92. Cloud Foundry open PaaS - Choice of clouds
A void
ck-in
Ap
e
Lo
fac
Private
p
Data
lica
Services
ter
Clouds
'o
r
In
n
S
ide
erv
Public
ov
Msg
ice
Services
Pr
Clouds Partners
In
ud
ter
Cl o
.COM
fac
Other Micro
e
Services
Clouds
Wednesday, March 28, 12
93. Cloud Foundry open PaaS - Choice of clouds
OSS community
A void
ck-in
Ap
e
Lo
fac
Private
p
Data
lica
Services
ter
Clouds
'o
r
In
n
S
ide
erv
Public
ov
Msg
ice
Services
Pr
Clouds Partners
In
ud
ter
Cl o
.COM
fac
Other Micro
e
Services
Clouds
Wednesday, March 28, 12
94. Open Source Advantage: it gets better faster!
§ http://code.google.com/p/googleappengine/issues/detail?id=13
• https://github.com/cloudfoundry/vcap/pull/25
50
Wednesday, March 28, 12
95. 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
51
Wednesday, March 28, 12
97. Node.js on Cloud foundry
(Demos and Examples)
53
Wednesday, March 28, 12
98. Hello World App on Cloud Foundry
//Simple http server on localhost //Simple http server on Cloud Foundry
var http = require('http'); var http = require('http');
http.createServer(function (req, res) { var host = process.env.VCAP_APP_HOST ||
res.writeHead(200, {'Content-Type': 'text/plain'}); ‘localhost’;
res.end('Hello Worldn'); var port = process.env.VCAP_APP_PORT || ‘3000’;
}).listen(3000, '127.0.0.1'); http.createServer(function (req, res) {
console.log('Server running at 127.0.0.1:3000'); res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello Worldn');
}).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
variable
2. 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
54
Wednesday, March 28, 12
99. Hello World App using ExpressJS on Cloud Foundry
var host = process.env.VCAP_APP_HOST || 'localhost'; Things to note:
var port = process.env.VCAP_APP_PORT || '3000';
1. ExpressJS is Ruby Sinatra
var express = require('express'); inspired web framework
var app = express.createServer();
2. It provides support for:
app.configure('development', function() { 1. Dev/Prod Configurations
app.use(express.errorHandler({ dumpExceptions: true, 2. Routes
showStack: true }));
}); 3. Templating
4. Sessions
app.configure('production', function() { 5. Many, many other features
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);
55
Wednesday, March 28, 12
100. Hello World App w/ built-in node.js cluster ( > 0.6)
var cluster = require('cluster'); Things to note:
var http = require('http'); 1. Node.js started to support Windows servers
var numCPUs = require('os').cpus().length;
var port = process.env.VCAP_APP_PORT || 3000;
• Supposedly runs faster on Windows J
var host = process.env.VCAP_APP_HOST ||
'localhost'; 1. If you are using cluster or need node 0.6 or
greater, use node06 runtime
if (cluster.isMaster) { //fork workers i.e. vmc push myapp --runtime=node06
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
} 2. This example spawns 4 node processes per
cluster.on('death', function(worker) { instance!
console.log('worker ' + worker.pid + ' died'); • Watch out, it might exceeds default 64MB
}); memory.
} else {
// Each worker processes have a http server. 3. You may have to implement Redis based-
http.Server(function(req, res) { session store
res.writeHead(200);
res.end(host + ":" + port);
}).listen(port, host);
console.log("listening on.. " + host + ":" +
port);
}
56
Wednesday, March 28, 12
101. 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
57
Wednesday, March 28, 12
102. Cloud Foundry Resources
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
58
Wednesday, March 28, 12
104. Thank You!
t @CloudFoundry
t @chanezon
Website : www.cloudfoundry.com
Blog : blog.cloudfoundry.com
Forum : support.cloudfoundry.com
Wednesday, March 28, 12
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
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 Jungle
http://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
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