SlideShare a Scribd company logo
1 of 70
Download to read offline
Becoming a Node.js ninja on
Cloud Foundry
 Raja Rao DV (@rajaraodv)
 Andy Piper (@andypiper)
 Cloud Foundry Developer Advocates




May 2012                       www.cloudfoundry.com
                                                      © 2009 VMware Inc. All rights reserved
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)
   • npm & package.json
   • Node.js EventEmitters

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




2
About Node.js


Node.js is a platform to build fast and scalable network applications. It is
built on Google Chrome’s 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 Joyent




                                                             Ryan Dahl
                                                           (Node.js creator)




    3
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




4
The cost of I/O




              http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/




5
So how does Node.js save I/O cost?




           Node.js saves I/O cost by implementing
            event driven, non-blocking I/O model




6
Event-driven, non-blocking I/O platform/server


What exactly is a event-driven, non-blocking platform/server?

How is it different from a multi-threaded platform/server?



                    Multi-threaded blocking server
                                  v/s
                   Event-driven, non-blocking server




  7
Multi-threaded server - Threads are spawned for every connection



    User1             I/O request         T1
                                                                        DB


                                                             Blocking




                                                                        FS


                                            Multi threaded
                                                server

    T   Thread




8
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




8
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
                                           T3   T4 T5


                                                                         FS


                                             Multi threaded
                                                 server

       T   Thread




   8
Multi-threaded server - Threads are spawned for every connection



       User1                            I/O request             T1
                                                                                               DB
       User2                            I/O request             T2
                                                                                    Blocking
                                                                                      I/O
      User3
refreshes 2 times
                                                                T3   T4 T5


                                                                     T7                        FS
      User4                                                     T6        T8   T9
refreshes 3 times


                                                                 Multi threaded
                                                                     server

       T   Thread

                Because every I/O request is blocking, server
                spawns a thread per connection to support
                multiple requests



   8
Non-blocking & Evented I/O (Node.js server)

                                                JS                   C/C++


                                                                     Libio
                                                                    POSIX
                                                                    Async
                                                                               DB
                                       T1
                                       V8                          Threads
                                            Event loop
                                            (Libev)


                                                                         t2
                                                                   t1
                                            Single
                                            thread                        t4
                                            serves                 t3
                                            all users                    t6
                                                                    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


 9
Non-blocking & Evented I/O (Node.js server)

                                                            JS                       C/C++

                                                                    T1               Libio
     User1                                I/O request               V8              POSIX
                                                                                    Async
                                                                                               DB
                                                                                   Threads
                                                        Event loop
                                                        (Libev)


                                                                                         t2
                                                                                   t1
                                                        Single
                                                        thread                            t4
                                                        serves                     t3
                                                        all users                        t6
                                                                                    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


 9
Non-blocking & Evented I/O (Node.js server)

                                                            JS                          C/C++

                                                                    T1                  Libio
     User1                                I/O request               V8                 POSIX
                                                                                       Async
                                                                                                             DB
                                                                                      Threads
                                                        Event loop
                                                        (Libev)


                                                                    delegate I/O to               Non-blocking
                                                                                            t2       I/O
                                                                        libeio        t1
                                                        Single
                                                        thread                               t4
                                                        serves                        t3
                                                        all users                           t6
                                                                                       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


 9
Non-blocking & Evented I/O (Node.js server)

                                                            JS                          C/C++

                                                                    T1                  Libio
     User1                                I/O request               V8                 POSIX
                                                                                       Async
                                                                                                             DB
                                                                                      Threads
                                                        Event loop
     User2                                I/O request   (Libev)


                                                                    delegate I/O to               Non-blocking
                                                                                            t2       I/O
                                                                        libeio        t1
                                                        Single
                                                        thread                               t4
                                                        serves                        t3
                                                        all users                           t6
                                                                                       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


 9
Non-blocking & Evented I/O (Node.js server)

                                                                 JS                           C/C++


     User1                                I/O request                                         Libio
                                                                                             POSIX
                                                                                             Async
                                                                                                                    DB
                                                                                            Threads
                                                             Event loop
     User2                                I/O request        (Libev)


                                                        T1               delegate I/O to                 Non-blocking
                                                                                                    t2      I/O
                                                        V8                   libeio          t1
                                                             Single
                                                             thread                                 t4
                                                             serves                          t3
                                                             all users                             t6
                                                                                             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


 9
Non-blocking & Evented I/O (Node.js server)

                                                                               JS                           C/C++


       User1                                I/O request                                                     Libio
                                                                                                           POSIX
                                                                                                           Async
                                                                                                                                  DB
                                                                                                          Threads
                                                                           Event loop
       User2                                I/O request                    (Libev)


                                                                     T1                delegate I/O to                 Non-blocking
                                                                                                                  t2      I/O
                                                                     V8                    libeio
      User3                                                                                                t1
                                                                           Single
refreshes 2 times                                                          thread                                 t4
                                                                           serves                          t3
                                                                           all users                             t6

      User4                                                                                                t5
refreshes 3 times
                                                                                                                  t7              FS
                                                                                                 I/O result returned
                                                                      T1                          2 EL after x time
                                                                      V8
  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 run in parallel (by libio)


   9
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)




 10
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


11
Apache v/s Nginx: Memory usage




                      Memory v/s concurrent connections


                   At ~4000 concurrent connections,
                   - Nginx uses 3MB memory
                   - Apache uses 40MB memory
             Ref: http://blog.webfaction.com/a-little-holiday-present


12
Saving I/O is great, what else is happening w/ Node.js?




     Let’s look at community, libraries, buzz around Node.js




13
Other things going on for Node.js

                   2nd most popular watched on Github




14
Other things going on for Node.js

                                9,000+ libraries/modules/servers




                                      High-level library categories
 Web frameworks        Oracle                              Multiple protocols          Command Line Option Parsers
 Routers               NoSQL and Key/Value                 HTTP                        Parser Generators
 Static file servers   Mongo                               FTP                         Other Parsers
                       Hive                                E-mail                      Debugging / Console Utilities
 Microframeworks
                       Redis                               XMPP                        Compression
 Frameworks            CouchDB                             Other networking            Graphics
 Middleware            Other NoSQL implementations         RPC                         Sound
 JSGI                  Miscellaneous and multiple DB       Web Sockets & Ajax          Payment Gateways
 Connect               Templating                          Message Queues              API clients
 Other middleware      CSS Engines                         Class systems               Control flow / Async goodies
 Other                 Content Management Systems          Testing / Spec Frameworks   I18n and L10n modules
 Database              Build and Deployment                Wrappers                    Boilerplates
                       Package Management Systems          Parsers                     Continuous Integration Tools
 MS SQL Server
                       Module Loader                       JSON                        DDD, CQRS, EventSourcing
 PostgreSQL            OpenSSL / Crypto / Hashing          XML                         Desktop application related
 MySQL                 SMTP                                                            JavaScript threads
 SQLite                TCP / IP                                                        Other




                                 https://github.com/joyent/node/wiki/modules



15
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
     • LdapJS.org - Node.js implementation of LDAP server itself
     • SMTP - Node.js implementation of SMTP server itself
     • XMPP, SSH, RPC, many more.




16
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)
   • npm & package.json
   • Node.js EventEmitters

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




17
Let’s look at the code...




How does async code differ from sync (regular) code?




                           Synchronous code
                                  v/s
                          Asynchronous Code




  18
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
                                              }

                                              //pass callback function to consume the result
                                              getItemNameById(100, displayHelperCallback); //step 1
Things to note:
1. Async code doesn’t directly ‘return’
   anything
2. Instead, it takes a function(callback) &
   calls that function when result
   becomes available



   19
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
getItemNameById(100, displayHelperCallback); //step 1
                                                                             //step 5
                                                                             V8 is free to run other functions in the event-loop.

//INTERNALS OF A DB LIBRARY (HIGH LEVEL)

function db() {
  this.dbConnection = net.connection(); // connects to DB
}
db.protorype.get = function(id, callback) {
   var self = this; //step 3 & //step4 is dbConnection.read (below)          //step 5, step 6 ..step 100
   this.dbConnection.read(id, function(result, callback) {
    self. receiveFromDB(result, callback);//step 101
                                                                    Step 5   Say 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
db.protorype.receiveFromDB = function(result, callback) {                    result & asks to run dbConnection.write’s callback.
  callback(result); //Execute callback step step 102
}
                                                                             This event goes to the back of the queue as step
                                                                             101


   20
Node.js Programming




How can I better organize my code?




                   Classes & CommonJS module




  21
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) {
       Automobile.call(this, license, model);
     }

     util.inherits(Car, Automobile);




22
CommonJS modules
//Automobile.js file                                      Things to note:
function Automobile(license, model) {                     1. Allows keeping JS code in separate
  this.license = license;
  this.model = model;                                        files
}
                                                          2. Use “exports.<name>” to export
Automobile.prototype.getModel = function() {                 something
  return model;
}
exports.Automobile = Automobile;                          1. Use require(‘path/to/module’) to
                                                             import it

                                                          2. use require(‘module’).<name> to
//Car.js file                                                access things inside module
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




   23
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’




   24
CommonJS modules: ‘exports’ v/s ‘module.exports’
//myModule.js file                                         Things to note:
module.exports = function () {                                 If you want to export only one class/
  return ‘hi there’;
}                                                              function.. so that it can be used
                                                               directly by the recipient, 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’




   25
Installing external modules – npm (Node Package Manager)
                    Use npm (Node Package Manager) to install modules
                    npm install <moduleName>
                    e.x. npm install express

                    Modules are copied into ./node_modules folder
                    /myapp
                    /myapp/node_modules/express



     Things to note:
     1. npm = Node Package Manager

     2. It is a CLI to install modules from http://search.npmjs.org

     3. LOCAL: npm install express
         1. It installs in myapp/node_modules/express

     4. GLOBAL: npm install express -g
         1. It installs in /usr/local/lib/node_modules/ (default)
         2. Installs executable files in /usr/local/.bin (default)

     5. Use GLOBAL when the library has some shell script & want to reuse it for
        different apps


26
Installing external modules - npm & package.json

//Instead keep ALL dependencies in           Things to note:
                                             1. If you use package.json, you can
package.json file in root of your app and       simply do npm install (w/o any
run:                                            module names)
npm install
                                             2. Keep package.json in root directory
//package.json
                                             3. Using package.json is preferred over
{                                               individual npm install <module>
  "name": "MyApp",
  "description": "My awesome twitter app",   4. You need to have all the modules pre-
  "version": "2.5.8",                           installed (i.e. npm install) before
                                                uploading your app to Cloud Foundry
  "author": "Raja <rajar@vmware.com>",
  "dependencies": {
    "express": "2.3.4",
    "mime": "",
    "connect-redis": ">= 0.0.1"
  }
}



27
Node.js EventEmitter (A utility class that allows emitting events)



     EventEmitter class implements the Observer pattern, and provides
     on and emit APIs
     - It is used when creating (not using) an async library.




                          Node.js EventEmitter




28
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;



 29
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




30
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');




31
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’);
     }




32
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 (or callback). 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
     }




33
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)
   • npm & package.json
   • Node.js EventEmitters

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




34
Cloud Foundry




       Cloud Foundry – Open Platform as a Service




35
Cloud Foundry open Platform as a Service
The PaaS of choice for the Cloud era

Simple
 • Lets 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




36
Cloud Foundry open PaaS - Choice of frameworks

                                                 OSS community
Cloud Foundry open PaaS - Choice of application services




vFabric
Postgres
                           Ap
                              plica

           Data Services
                                   'o
                                     n *S


     vFabric
     RabbitMQTM
                                        erv



                    Msg Services
                                           ice
                                              *In
                                              ter
                                                 fac




                                    Other
                                                    e




                                   Services

            Additional partners services
            …
Cloud Foundry open PaaS - Choice of clouds




                                                                                      Av oid
                                                                                      Loc k-in




                                                                     e
                       Ap




                                                                  fac
                                                                           Private*
                          p




                                                               ter
                           lica

       Data Services
                                                                           Clouds*




                                                               r*In
                               'o




                                                            ide
                                 n *S




                                                          ov
                                    erv



                                                                      Public


                                                       *Pr
                Msg Services
                                       ice



                                                                      Clouds            Partners

                                                     ud
                                          *In




                                                    Cl o
                                          ter




                                                                                      .COM
                                                            Micro
                                             fac




                                Other
                                                e




                               Services
                                                            Clouds
Node.js & Cloud Foundry
        (Demos and Examples)




40
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 || ‘localhost’;
  res.writeHead(200, {'Content-Type': 'text/plain'});   var port = process.env.VCAP_APP_PORT || ‘3000’;
  res.end('Hello Worldn');                             http.createServer(function (req, res) {
}).listen(3000, '127.0.0.1');                             res.writeHead(200, {'Content-Type': 'text/plain'});
console.log('Server running at 127.0.0.1:3000');          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 and have it
         recognised by vmc push deployment




    41
“cloudfoundry” module & Connecting to MongoDB, Redis




             Connecting to services




42
“cloudfoundry” NodeJS helper module
npm install cloudfoundry

var cloudfoundry = require(‘cloudfoundry’);

cloudfoundry.cloud //is running on Cloud Foundry?
cloudfoundry.host // host
cloudfoundry.port // port

//Example: Say you are using ‘test-mongodb’ MongoDB service, you can get its info:
cloudfoundry.mongodb['test-mongodb'].credentials.hostname
cloudfoundry.mongodb['test-mongodb'].credentials.port
cloudfoundry.mongodb['test-mongodb'].credentials.db
cloudfoundry.mongodb['test-mongodb'].credentials.username
cloudfoundry.mongodb['test-mongodb'].credentials.password



     Things to note:
     1. cloudfoundry module (built by ‘igo’) provides easy access to environment variables

     2. For more, please go through https://github.com/cloudfoundry-samples/cloudfoundry




43
MongoDB – Example of inserting a user (using mongodb-native module)

     var mongodb = require('mongodb').Db;
     var conn; // holds connection

     //connect to db and get connection obj
     //connectionUrl looks like mongodb://username:pwd@host:port/dbName
     mongodb.connect(connectionUrl, function(err, connection) {
           conn = connection;
     }

     //add a user
     function addUser (userObj, callback) {
            //Get the collection that holds users
            conn.collection('users', function (err, userCollection) {
                 //insert user to this collection
                    userCollection.insert(userObj, {safe:true}, function(err) {
                                  callback(userObj);
                    });
            });
     }

     //PS: Error handling is not shown




44
Demo app




     Things to note:
     1. Simple MongoDB demo app, adds random users and pulls existing users

     2. https://github.com/rajaraodv/mongoapp1


45
ExpressJS




46
Hello World App using ExpressJS
var host = process.env.VCAP_APP_HOST || 'localhost';
var port = process.env.VCAP_APP_PORT || '3000';
                                                       Things to note:
                                                       1. ExpressJS is a Ruby Sinatra
var express = require('express');
var app = express.createServer();
                                                          inspired web framework
app.listen(port, host);
                                                       2. Built on top of ‘Connect’ –
                                                         which itself is a wrapper for
                                                         http-module

                                                       3. Provides support for:
                                                          1. Dev/Prod Configurations
                                                          2. Routes
                                                          3. Templating
                                                          4. Sessions
                                                          5. Many, many other features




47
Hello World App using ExpressJS (Middlewares)

     var express = require('express');
     var app = express.createServer();

     //Middlewares
     app.use(express.logger()); //logs requests
     app.use(express.static(__dirname + ‘/public’)); //sets location of public files
     app.use(express.bodyParser()); //parses HTTP POST body




        Things to Note:
       1. Middlewares are functions that helps in common tasks involved building in
          web applications
       2. They are actually connect-module functions but exposed by ExpressJS for
          simplicity




48
Hello World App using ExpressJS (Environments)

     var express = require('express');
     var app = express.createServer();

     app.configure('development', function() {
      //On error, print exceptions to console & to the web-page itself
      app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
     });

     app.configure('production', function() {
       //On error, this simply says ‘Internal error Occurred’
            app.use(express.errorHandler({ dumpExceptions: true, showStack: false }));
            app.use(express.logger()); //logs requests
     });



         Things to Note:
        1. Express uses NODE_ENV environment variable to set working environment
        2. On CF, to toggle b/w ‘development’ and ‘production’, you can use..
             vmc env-add <appName> NODE_ENV ‘production’




49
Hello World App using ExpressJS (Routing)
var express = require('express');                         Things to note:
var app = express.createServer();                           1. You can use Routing to listen
app.use(express.bodyParser());                                 to requests to call different
                                                               functions
//Receive HTTP GET
app.get('/user', function(req, res) {                       2. You can listen to HTTP POST,
 //Don’t let people call to /user                              PUT etc.
  throw new Error(“I’m private. Call me w/ a user id");
});

app.get('/user/:id', function(req, res){
    res.send('user ' + req.params.id);
});

//Receive HTTP POST
app.post(‘’/register”, function(req, res) {
 //(Use bodyParser middleware for this)
  var body = req.body;
  db.save(body.user, body.password);
}




50
Hello World App using ExpressJS (Sessions)

     var express = require('express');
     var app = express.createServer();

     //Middlewares
     app.use(express.static(__dirname + ‘/public’)); //sets location of public files
     app.use(express.bodyParser()); //parses HTTP POST body
     app.use(express.cookieParser()); //Parses cookies headers
     app.use(express.session({secret: 'your secret here}));




     Things to note:
        1. To create sessions, use cookieParser & session middlewares

        2. By default Express uses MemoryStore to store sessions

        3. You can use Redis to store sessions




51
ExpressJS (Sticky sessions for multi instances)

     var express = require('express');
     var app = express.createServer();

     //Middlewares
     app.use(express.static(__dirname + ‘/public’)); //sets location of public files
     app.use(express.bodyParser()); //parses HTTP POST body
     app.use(express.cookieParser()); //Parses cookies headers
     app.use(express.session({secret: 'your secret here’, key: ‘jsessionid’ }));



     Things to note:
        1. Sticky Session is a reverse proxy / load balancer feature to help persistent
           connection

        2. When Sticky Session is on, request goes from Nginx to the same instance no
           matter how many instances of your app is running .

        3. Cloud Foundry’s Nginx provides Sticky Sessions on ‘jsessionid’ cookie

        4. W/o setting this requests are randomly sent to different instances & you’ll have
           to use external store like Redis to fetch session data (recommended).



52
ExpressJS (demo)




53
ExpressJS demo app screenshots (routing, sessions & sticky sessions)




     Demo Explains how session, sticky sessions, routing etc. works
     For more: https://github.com/rajaraodv/express1



54
Socket.io on Cloud Foundry




                       Socket.io




55
Socket.io on Cloud Foundry (server side)

     var sio = require('socket.io');
     var express = require('express');
     var app = express.createServer();

     var io = sio.listen(app);//listen to express
     io.configure(function() {
         io.set('log level', 1);
         io.set("transports", ["xhr-polling"]); //Currently CF doesn’t support websockets
     });




         Things to Note:
       1. Socket.io is mainly used to build real-time apps

       2. Socket.io provides a single interface to switch b/w various transport techniques like xhr-polling,
          websocket, JSONP etc

       3. In addition, it provides heartbeats, reconnection, timeouts etc. that are vital for real-time apps.
       4. It works seamlessly with ExpressJS




56
Socket.io on Cloud Foundry (server side continued)

     //After listening to express..wait for connection from browser

     io.sockets.on('connection',
     function(socket) {
         // When the client/browser emits 'sendchat', this listens and executes
         socket.on('sendchat',
         function(data) {
             // We will echo it back to ALL sockets
             io.sockets.emit('updatechat’, data);
         });
     });




57
Socket.io on Cloud Foundry (client side)

<script src="/socket.io/socket.io.js"></script>//socket.io serves this file from server

var socket = io.connect(document.location.href); //connect to the server

     // on connection
     socket.on('connect', function() {
                console.log("client connected");
     });

     // Whenever the server emits 'updatechat', this updates the chat body
     socket.on('updatechat', function (data) {
         $('#conversation').append(data); // append it to my list
     });

  //When the user enter some data, send it to server
  function sendchat() {
        var message = $('#chatField').val();

            // Emit or tell server to execute 'sendchat’
           socket.emit('sendchat', message);
      }




58
Socket.io (+ ExpressJS) Demo app screenshots




                                     For more:
                                     https://github.com/rajaraodv/socketio1




59
Simple coding and deployment - Cloud9 IDE




60
Summary


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)
   • npm & package.json
   • Node.js EventEmitters

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




61
Questions?




                       Questions?
               @rajaraodv (github.com/rajaraodv)
               @andypiper (github.com/andypiper)

             @cloudfoundry (github.com/cloudfoundry)




62

More Related Content

Similar to Becoming a Node.js Ninja on Cloud Foundry - Open Tour London

Cloud Foundry Open Tour Beijing: Becoming a Node.js Ninja on
 Cloud Foundry
Cloud Foundry Open Tour Beijing: Becoming a Node.js Ninja on
 Cloud FoundryCloud Foundry Open Tour Beijing: Becoming a Node.js Ninja on
 Cloud Foundry
Cloud Foundry Open Tour Beijing: Becoming a Node.js Ninja on
 Cloud FoundryPatrick Chanezon
 
Zero-Copy Event-Driven Servers with Netty
Zero-Copy Event-Driven Servers with NettyZero-Copy Event-Driven Servers with Netty
Zero-Copy Event-Driven Servers with NettyDaniel Bimschas
 
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」Sho Shimizu
 
I3 multicore processor
I3 multicore processorI3 multicore processor
I3 multicore processorAmol Barewar
 
The Evolution of Java Persistence in EclipseLink: Shaun Smith
The Evolution of Java Persistence in EclipseLink: Shaun SmithThe Evolution of Java Persistence in EclipseLink: Shaun Smith
The Evolution of Java Persistence in EclipseLink: Shaun Smithjaxconf
 
Multi core-architecture
Multi core-architectureMulti core-architecture
Multi core-architecturePiyush Mittal
 
layeringanjanv1-090522005231-phpapp01.pdf
layeringanjanv1-090522005231-phpapp01.pdflayeringanjanv1-090522005231-phpapp01.pdf
layeringanjanv1-090522005231-phpapp01.pdfImXaib
 
Comparison of mqtt and coap protocol
Comparison of mqtt and coap protocolComparison of mqtt and coap protocol
Comparison of mqtt and coap protocolYUSUF HUMAYUN
 
Current state of IEEE 802.1 Time-Sensitive Networking Task Group Norman Finn,...
Current state of IEEE 802.1 Time-Sensitive Networking Task Group Norman Finn,...Current state of IEEE 802.1 Time-Sensitive Networking Task Group Norman Finn,...
Current state of IEEE 802.1 Time-Sensitive Networking Task Group Norman Finn,...Jörgen Gade
 
CloudStackユーザ会〜仮想ルータの謎に迫る
CloudStackユーザ会〜仮想ルータの謎に迫るCloudStackユーザ会〜仮想ルータの謎に迫る
CloudStackユーザ会〜仮想ルータの謎に迫るsamemoon
 
CloudStackユーザ会〜仮想ルータの謎に迫る
CloudStackユーザ会〜仮想ルータの謎に迫るCloudStackユーザ会〜仮想ルータの謎に迫る
CloudStackユーザ会〜仮想ルータの謎に迫るsamemoon
 
Internet protocol
Internet protocolInternet protocol
Internet protocolOnline
 

Similar to Becoming a Node.js Ninja on Cloud Foundry - Open Tour London (20)

Cloud Foundry Open Tour Beijing: Becoming a Node.js Ninja on
 Cloud Foundry
Cloud Foundry Open Tour Beijing: Becoming a Node.js Ninja on
 Cloud FoundryCloud Foundry Open Tour Beijing: Becoming a Node.js Ninja on
 Cloud Foundry
Cloud Foundry Open Tour Beijing: Becoming a Node.js Ninja on
 Cloud Foundry
 
Web Presen
Web PresenWeb Presen
Web Presen
 
Zero-Copy Event-Driven Servers with Netty
Zero-Copy Event-Driven Servers with NettyZero-Copy Event-Driven Servers with Netty
Zero-Copy Event-Driven Servers with Netty
 
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
Openflow勉強会 「OpenFlowコントローラを取り巻く状況とその実装」
 
I3 multicore processor
I3 multicore processorI3 multicore processor
I3 multicore processor
 
I3
I3I3
I3
 
The Evolution of Java Persistence in EclipseLink: Shaun Smith
The Evolution of Java Persistence in EclipseLink: Shaun SmithThe Evolution of Java Persistence in EclipseLink: Shaun Smith
The Evolution of Java Persistence in EclipseLink: Shaun Smith
 
Multi core-architecture
Multi core-architectureMulti core-architecture
Multi core-architecture
 
CloverETL Training Sample
CloverETL Training SampleCloverETL Training Sample
CloverETL Training Sample
 
layeringanjanv1-090522005231-phpapp01.pdf
layeringanjanv1-090522005231-phpapp01.pdflayeringanjanv1-090522005231-phpapp01.pdf
layeringanjanv1-090522005231-phpapp01.pdf
 
Comparison of mqtt and coap protocol
Comparison of mqtt and coap protocolComparison of mqtt and coap protocol
Comparison of mqtt and coap protocol
 
Current state of IEEE 802.1 Time-Sensitive Networking Task Group Norman Finn,...
Current state of IEEE 802.1 Time-Sensitive Networking Task Group Norman Finn,...Current state of IEEE 802.1 Time-Sensitive Networking Task Group Norman Finn,...
Current state of IEEE 802.1 Time-Sensitive Networking Task Group Norman Finn,...
 
Parallel Programming
Parallel ProgrammingParallel Programming
Parallel Programming
 
Np unit1
Np unit1Np unit1
Np unit1
 
CloudStackユーザ会〜仮想ルータの謎に迫る
CloudStackユーザ会〜仮想ルータの謎に迫るCloudStackユーザ会〜仮想ルータの謎に迫る
CloudStackユーザ会〜仮想ルータの謎に迫る
 
CloudStackユーザ会〜仮想ルータの謎に迫る
CloudStackユーザ会〜仮想ルータの謎に迫るCloudStackユーザ会〜仮想ルータの謎に迫る
CloudStackユーザ会〜仮想ルータの謎に迫る
 
Ethernet technology
Ethernet technologyEthernet technology
Ethernet technology
 
TELNET Protocol
TELNET ProtocolTELNET Protocol
TELNET Protocol
 
Network programming
Network programmingNetwork programming
Network programming
 
Internet protocol
Internet protocolInternet protocol
Internet protocol
 

More from Andy Piper

Adapt & Survive
Adapt & SurviveAdapt & Survive
Adapt & SurviveAndy Piper
 
Rebooting A Community #DevRelCon
Rebooting A Community #DevRelConRebooting A Community #DevRelCon
Rebooting A Community #DevRelConAndy Piper
 
Twitter APIs for #MediaHackday
Twitter APIs for #MediaHackdayTwitter APIs for #MediaHackday
Twitter APIs for #MediaHackdayAndy Piper
 
Imagining the Future, when the Future is already Now
Imagining the Future, when the Future is already NowImagining the Future, when the Future is already Now
Imagining the Future, when the Future is already NowAndy Piper
 
Connecting to the Pulse of the Planet with the Twitter Platform
Connecting to the Pulse of the Planet with the Twitter PlatformConnecting to the Pulse of the Planet with the Twitter Platform
Connecting to the Pulse of the Planet with the Twitter PlatformAndy Piper
 
Building Twitter's SDKs for Android
Building Twitter's SDKs for AndroidBuilding Twitter's SDKs for Android
Building Twitter's SDKs for AndroidAndy Piper
 
Developer Advocacy - A Life Less Ordinary
Developer Advocacy - A Life Less OrdinaryDeveloper Advocacy - A Life Less Ordinary
Developer Advocacy - A Life Less OrdinaryAndy Piper
 
Twitter in the Internet of Things
Twitter in the Internet of ThingsTwitter in the Internet of Things
Twitter in the Internet of ThingsAndy Piper
 
Twitter APIs - the starter guide
Twitter APIs - the starter guideTwitter APIs - the starter guide
Twitter APIs - the starter guideAndy Piper
 
Connecting to the pulse of the planet with Twitter APIs
Connecting to the pulse of the planet with Twitter APIsConnecting to the pulse of the planet with Twitter APIs
Connecting to the pulse of the planet with Twitter APIsAndy Piper
 
Internet ALL the Things - a walking tour of MQTT
Internet ALL the Things - a walking tour of MQTTInternet ALL the Things - a walking tour of MQTT
Internet ALL the Things - a walking tour of MQTTAndy Piper
 
Combining Context with Signals in the IoT (longer version)
Combining Context with Signals in the IoT (longer version)Combining Context with Signals in the IoT (longer version)
Combining Context with Signals in the IoT (longer version)Andy Piper
 
Why the Internet of Things will be built on Open Source
Why the Internet of Things will be built on Open SourceWhy the Internet of Things will be built on Open Source
Why the Internet of Things will be built on Open SourceAndy Piper
 
Combining Context with Signals in the Internet of Things
Combining Context with Signals in the Internet of ThingsCombining Context with Signals in the Internet of Things
Combining Context with Signals in the Internet of ThingsAndy Piper
 
MQTT - standards-based plumbing for the Internet of Things
MQTT - standards-based plumbing for the Internet of ThingsMQTT - standards-based plumbing for the Internet of Things
MQTT - standards-based plumbing for the Internet of ThingsAndy Piper
 
My Quantified Self and the promise of wearables
My Quantified Self and the promise of wearablesMy Quantified Self and the promise of wearables
My Quantified Self and the promise of wearablesAndy Piper
 
Why Data, Code and Mobile converge in the Open Cloud
Why Data, Code and Mobile converge in the Open CloudWhy Data, Code and Mobile converge in the Open Cloud
Why Data, Code and Mobile converge in the Open CloudAndy Piper
 
From Cloud Computing to Platform as a Service – BCS Oxfordshire
From Cloud Computing to Platform as a Service – BCS OxfordshireFrom Cloud Computing to Platform as a Service – BCS Oxfordshire
From Cloud Computing to Platform as a Service – BCS OxfordshireAndy Piper
 
Why Apps, Data and Mobile Converge in the Open Cloud
Why Apps, Data and Mobile Converge in the Open CloudWhy Apps, Data and Mobile Converge in the Open Cloud
Why Apps, Data and Mobile Converge in the Open CloudAndy Piper
 
The Internet of Things is Made of Signals
The Internet of Things is Made of SignalsThe Internet of Things is Made of Signals
The Internet of Things is Made of SignalsAndy Piper
 

More from Andy Piper (20)

Adapt & Survive
Adapt & SurviveAdapt & Survive
Adapt & Survive
 
Rebooting A Community #DevRelCon
Rebooting A Community #DevRelConRebooting A Community #DevRelCon
Rebooting A Community #DevRelCon
 
Twitter APIs for #MediaHackday
Twitter APIs for #MediaHackdayTwitter APIs for #MediaHackday
Twitter APIs for #MediaHackday
 
Imagining the Future, when the Future is already Now
Imagining the Future, when the Future is already NowImagining the Future, when the Future is already Now
Imagining the Future, when the Future is already Now
 
Connecting to the Pulse of the Planet with the Twitter Platform
Connecting to the Pulse of the Planet with the Twitter PlatformConnecting to the Pulse of the Planet with the Twitter Platform
Connecting to the Pulse of the Planet with the Twitter Platform
 
Building Twitter's SDKs for Android
Building Twitter's SDKs for AndroidBuilding Twitter's SDKs for Android
Building Twitter's SDKs for Android
 
Developer Advocacy - A Life Less Ordinary
Developer Advocacy - A Life Less OrdinaryDeveloper Advocacy - A Life Less Ordinary
Developer Advocacy - A Life Less Ordinary
 
Twitter in the Internet of Things
Twitter in the Internet of ThingsTwitter in the Internet of Things
Twitter in the Internet of Things
 
Twitter APIs - the starter guide
Twitter APIs - the starter guideTwitter APIs - the starter guide
Twitter APIs - the starter guide
 
Connecting to the pulse of the planet with Twitter APIs
Connecting to the pulse of the planet with Twitter APIsConnecting to the pulse of the planet with Twitter APIs
Connecting to the pulse of the planet with Twitter APIs
 
Internet ALL the Things - a walking tour of MQTT
Internet ALL the Things - a walking tour of MQTTInternet ALL the Things - a walking tour of MQTT
Internet ALL the Things - a walking tour of MQTT
 
Combining Context with Signals in the IoT (longer version)
Combining Context with Signals in the IoT (longer version)Combining Context with Signals in the IoT (longer version)
Combining Context with Signals in the IoT (longer version)
 
Why the Internet of Things will be built on Open Source
Why the Internet of Things will be built on Open SourceWhy the Internet of Things will be built on Open Source
Why the Internet of Things will be built on Open Source
 
Combining Context with Signals in the Internet of Things
Combining Context with Signals in the Internet of ThingsCombining Context with Signals in the Internet of Things
Combining Context with Signals in the Internet of Things
 
MQTT - standards-based plumbing for the Internet of Things
MQTT - standards-based plumbing for the Internet of ThingsMQTT - standards-based plumbing for the Internet of Things
MQTT - standards-based plumbing for the Internet of Things
 
My Quantified Self and the promise of wearables
My Quantified Self and the promise of wearablesMy Quantified Self and the promise of wearables
My Quantified Self and the promise of wearables
 
Why Data, Code and Mobile converge in the Open Cloud
Why Data, Code and Mobile converge in the Open CloudWhy Data, Code and Mobile converge in the Open Cloud
Why Data, Code and Mobile converge in the Open Cloud
 
From Cloud Computing to Platform as a Service – BCS Oxfordshire
From Cloud Computing to Platform as a Service – BCS OxfordshireFrom Cloud Computing to Platform as a Service – BCS Oxfordshire
From Cloud Computing to Platform as a Service – BCS Oxfordshire
 
Why Apps, Data and Mobile Converge in the Open Cloud
Why Apps, Data and Mobile Converge in the Open CloudWhy Apps, Data and Mobile Converge in the Open Cloud
Why Apps, Data and Mobile Converge in the Open Cloud
 
The Internet of Things is Made of Signals
The Internet of Things is Made of SignalsThe Internet of Things is Made of Signals
The Internet of Things is Made of Signals
 

Recently uploaded

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraDeakin University
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphNeo4j
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 

Recently uploaded (20)

08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
Artificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning eraArtificial intelligence in the post-deep learning era
Artificial intelligence in the post-deep learning era
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge GraphSIEMENS: RAPUNZEL – A Tale About Knowledge Graph
SIEMENS: RAPUNZEL – A Tale About Knowledge Graph
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 

Becoming a Node.js Ninja on Cloud Foundry - Open Tour London

  • 1. Becoming a Node.js ninja on Cloud Foundry Raja Rao DV (@rajaraodv) Andy Piper (@andypiper) Cloud Foundry Developer Advocates May 2012 www.cloudfoundry.com © 2009 VMware Inc. All rights reserved
  • 2. 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) • npm & package.json • Node.js EventEmitters 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 2
  • 3. About Node.js Node.js is a platform to build fast and scalable network applications. It is built on Google Chrome’s 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 Joyent Ryan Dahl (Node.js creator) 3
  • 4. 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 4
  • 5. The cost of I/O http://blog.mixu.net/2011/02/01/understanding-the-node-js-event-loop/ 5
  • 6. So how does Node.js save I/O cost? Node.js saves I/O cost by implementing event driven, non-blocking I/O model 6
  • 7. Event-driven, non-blocking I/O platform/server What exactly is a event-driven, non-blocking platform/server? How is it different from a multi-threaded platform/server? Multi-threaded blocking server v/s Event-driven, non-blocking server 7
  • 8. Multi-threaded server - Threads are spawned for every connection User1 I/O request T1 DB Blocking FS Multi threaded server T Thread 8
  • 9. 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 8
  • 10. 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 T3 T4 T5 FS Multi threaded server T Thread 8
  • 11. Multi-threaded server - Threads are spawned for every connection User1 I/O request T1 DB User2 I/O request T2 Blocking I/O User3 refreshes 2 times T3 T4 T5 T7 FS User4 T6 T8 T9 refreshes 3 times Multi threaded server T Thread Because every I/O request is blocking, server spawns a thread per connection to support multiple requests 8
  • 12. Non-blocking & Evented I/O (Node.js server) JS C/C++ Libio POSIX Async DB T1 V8 Threads Event loop (Libev) t2 t1 Single thread t4 serves t3 all users t6 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 9
  • 13. Non-blocking & Evented I/O (Node.js server) JS C/C++ T1 Libio User1 I/O request V8 POSIX Async DB Threads Event loop (Libev) t2 t1 Single thread t4 serves t3 all users t6 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 9
  • 14. Non-blocking & Evented I/O (Node.js server) JS C/C++ T1 Libio User1 I/O request V8 POSIX Async DB Threads Event loop (Libev) delegate I/O to Non-blocking t2 I/O libeio t1 Single thread t4 serves t3 all users t6 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 9
  • 15. Non-blocking & Evented I/O (Node.js server) JS C/C++ T1 Libio User1 I/O request V8 POSIX Async DB Threads Event loop User2 I/O request (Libev) delegate I/O to Non-blocking t2 I/O libeio t1 Single thread t4 serves t3 all users t6 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 9
  • 16. Non-blocking & Evented I/O (Node.js server) JS C/C++ User1 I/O request Libio POSIX Async DB Threads Event loop User2 I/O request (Libev) T1 delegate I/O to Non-blocking t2 I/O V8 libeio t1 Single thread t4 serves t3 all users t6 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 9
  • 17. Non-blocking & Evented I/O (Node.js server) JS C/C++ User1 I/O request Libio POSIX Async DB Threads Event loop User2 I/O request (Libev) T1 delegate I/O to Non-blocking t2 I/O V8 libeio User3 t1 Single refreshes 2 times thread t4 serves t3 all users t6 User4 t5 refreshes 3 times t7 FS I/O result returned T1 2 EL after x time V8 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 run in parallel (by libio) 9
  • 18. 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) 10
  • 19. 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 11
  • 20. Apache v/s Nginx: Memory usage Memory v/s concurrent connections At ~4000 concurrent connections, - Nginx uses 3MB memory - Apache uses 40MB memory Ref: http://blog.webfaction.com/a-little-holiday-present 12
  • 21. Saving I/O is great, what else is happening w/ Node.js? Let’s look at community, libraries, buzz around Node.js 13
  • 22. Other things going on for Node.js 2nd most popular watched on Github 14
  • 23. Other things going on for Node.js 9,000+ libraries/modules/servers High-level library categories Web frameworks Oracle Multiple protocols Command Line Option Parsers Routers NoSQL and Key/Value HTTP Parser Generators Static file servers Mongo FTP Other Parsers Hive E-mail Debugging / Console Utilities Microframeworks Redis XMPP Compression Frameworks CouchDB Other networking Graphics Middleware Other NoSQL implementations RPC Sound JSGI Miscellaneous and multiple DB Web Sockets & Ajax Payment Gateways Connect Templating Message Queues API clients Other middleware CSS Engines Class systems Control flow / Async goodies Other Content Management Systems Testing / Spec Frameworks I18n and L10n modules Database Build and Deployment Wrappers Boilerplates Package Management Systems Parsers Continuous Integration Tools MS SQL Server Module Loader JSON DDD, CQRS, EventSourcing PostgreSQL OpenSSL / Crypto / Hashing XML Desktop application related MySQL SMTP JavaScript threads SQLite TCP / IP Other https://github.com/joyent/node/wiki/modules 15
  • 24. 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 • LdapJS.org - Node.js implementation of LDAP server itself • SMTP - Node.js implementation of SMTP server itself • XMPP, SSH, RPC, many more. 16
  • 25. 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) • npm & package.json • Node.js EventEmitters 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 17
  • 26. Let’s look at the code... How does async code differ from sync (regular) code? Synchronous code v/s Asynchronous Code 18
  • 27. 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 } //pass callback function to consume the result getItemNameById(100, displayHelperCallback); //step 1 Things to note: 1. Async code doesn’t directly ‘return’ anything 2. Instead, it takes a function(callback) & calls that function when result becomes available 19
  • 28. 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 getItemNameById(100, displayHelperCallback); //step 1 //step 5 V8 is free to run other functions in the event-loop. //INTERNALS OF A DB LIBRARY (HIGH LEVEL) function db() { this.dbConnection = net.connection(); // connects to DB } db.protorype.get = function(id, callback) { var self = this; //step 3 & //step4 is dbConnection.read (below) //step 5, step 6 ..step 100 this.dbConnection.read(id, function(result, callback) { self. receiveFromDB(result, callback);//step 101 Step 5 Say 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 db.protorype.receiveFromDB = function(result, callback) { result & asks to run dbConnection.write’s callback. callback(result); //Execute callback step step 102 } This event goes to the back of the queue as step 101 20
  • 29. Node.js Programming How can I better organize my code? Classes & CommonJS module 21
  • 30. 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) { Automobile.call(this, license, model); } util.inherits(Car, Automobile); 22
  • 31. CommonJS modules //Automobile.js file Things to note: function Automobile(license, model) { 1. Allows keeping JS code in separate this.license = license; this.model = model; files } 2. Use “exports.<name>” to export Automobile.prototype.getModel = function() { something return model; } exports.Automobile = Automobile; 1. Use require(‘path/to/module’) to import it 2. use require(‘module’).<name> to //Car.js file access things inside module 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 23
  • 32. 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’ 24
  • 33. CommonJS modules: ‘exports’ v/s ‘module.exports’ //myModule.js file Things to note: module.exports = function () { If you want to export only one class/ return ‘hi there’; } function.. so that it can be used directly by the recipient, 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’ 25
  • 34. Installing external modules – npm (Node Package Manager) Use npm (Node Package Manager) to install modules npm install <moduleName> e.x. npm install express Modules are copied into ./node_modules folder /myapp /myapp/node_modules/express Things to note: 1. npm = Node Package Manager 2. It is a CLI to install modules from http://search.npmjs.org 3. LOCAL: npm install express 1. It installs in myapp/node_modules/express 4. GLOBAL: npm install express -g 1. It installs in /usr/local/lib/node_modules/ (default) 2. Installs executable files in /usr/local/.bin (default) 5. Use GLOBAL when the library has some shell script & want to reuse it for different apps 26
  • 35. Installing external modules - npm & package.json //Instead keep ALL dependencies in Things to note: 1. If you use package.json, you can package.json file in root of your app and simply do npm install (w/o any run: module names) npm install 2. Keep package.json in root directory //package.json 3. Using package.json is preferred over { individual npm install <module> "name": "MyApp", "description": "My awesome twitter app", 4. You need to have all the modules pre- "version": "2.5.8", installed (i.e. npm install) before uploading your app to Cloud Foundry "author": "Raja <rajar@vmware.com>", "dependencies": { "express": "2.3.4", "mime": "", "connect-redis": ">= 0.0.1" } } 27
  • 36. Node.js EventEmitter (A utility class that allows emitting events) EventEmitter class implements the Observer pattern, and provides on and emit APIs - It is used when creating (not using) an async library. Node.js EventEmitter 28
  • 37. 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; 29
  • 38. 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 30
  • 39. 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'); 31
  • 40. 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’); } 32
  • 41. 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 (or callback). 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 } 33
  • 42. 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) • npm & package.json • Node.js EventEmitters 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 34
  • 43. Cloud Foundry Cloud Foundry – Open Platform as a Service 35
  • 44. Cloud Foundry open Platform as a Service The PaaS of choice for the Cloud era Simple • Lets 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 36
  • 45. Cloud Foundry open PaaS - Choice of frameworks OSS community
  • 46. Cloud Foundry open PaaS - Choice of application services vFabric Postgres Ap plica Data Services 'o n *S vFabric RabbitMQTM erv Msg Services ice *In ter fac Other e Services Additional partners services …
  • 47. Cloud Foundry open PaaS - Choice of clouds Av oid Loc k-in e Ap fac Private* p ter lica Data Services Clouds* r*In 'o ide n *S ov erv Public *Pr Msg Services ice Clouds Partners ud *In Cl o ter .COM Micro fac Other e Services Clouds
  • 48. Node.js & Cloud Foundry (Demos and Examples) 40
  • 49. 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 || ‘localhost’; res.writeHead(200, {'Content-Type': 'text/plain'}); var port = process.env.VCAP_APP_PORT || ‘3000’; res.end('Hello Worldn'); http.createServer(function (req, res) { }).listen(3000, '127.0.0.1'); res.writeHead(200, {'Content-Type': 'text/plain'}); console.log('Server running at 127.0.0.1:3000'); 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 and have it recognised by vmc push deployment 41
  • 50. “cloudfoundry” module & Connecting to MongoDB, Redis Connecting to services 42
  • 51. “cloudfoundry” NodeJS helper module npm install cloudfoundry var cloudfoundry = require(‘cloudfoundry’); cloudfoundry.cloud //is running on Cloud Foundry? cloudfoundry.host // host cloudfoundry.port // port //Example: Say you are using ‘test-mongodb’ MongoDB service, you can get its info: cloudfoundry.mongodb['test-mongodb'].credentials.hostname cloudfoundry.mongodb['test-mongodb'].credentials.port cloudfoundry.mongodb['test-mongodb'].credentials.db cloudfoundry.mongodb['test-mongodb'].credentials.username cloudfoundry.mongodb['test-mongodb'].credentials.password Things to note: 1. cloudfoundry module (built by ‘igo’) provides easy access to environment variables 2. For more, please go through https://github.com/cloudfoundry-samples/cloudfoundry 43
  • 52. MongoDB – Example of inserting a user (using mongodb-native module) var mongodb = require('mongodb').Db; var conn; // holds connection //connect to db and get connection obj //connectionUrl looks like mongodb://username:pwd@host:port/dbName mongodb.connect(connectionUrl, function(err, connection) { conn = connection; } //add a user function addUser (userObj, callback) { //Get the collection that holds users conn.collection('users', function (err, userCollection) { //insert user to this collection userCollection.insert(userObj, {safe:true}, function(err) { callback(userObj); }); }); } //PS: Error handling is not shown 44
  • 53. Demo app Things to note: 1. Simple MongoDB demo app, adds random users and pulls existing users 2. https://github.com/rajaraodv/mongoapp1 45
  • 55. Hello World App using ExpressJS var host = process.env.VCAP_APP_HOST || 'localhost'; var port = process.env.VCAP_APP_PORT || '3000'; Things to note: 1. ExpressJS is a Ruby Sinatra var express = require('express'); var app = express.createServer(); inspired web framework app.listen(port, host); 2. Built on top of ‘Connect’ – which itself is a wrapper for http-module 3. Provides support for: 1. Dev/Prod Configurations 2. Routes 3. Templating 4. Sessions 5. Many, many other features 47
  • 56. Hello World App using ExpressJS (Middlewares) var express = require('express'); var app = express.createServer(); //Middlewares app.use(express.logger()); //logs requests app.use(express.static(__dirname + ‘/public’)); //sets location of public files app.use(express.bodyParser()); //parses HTTP POST body Things to Note: 1. Middlewares are functions that helps in common tasks involved building in web applications 2. They are actually connect-module functions but exposed by ExpressJS for simplicity 48
  • 57. Hello World App using ExpressJS (Environments) var express = require('express'); var app = express.createServer(); app.configure('development', function() { //On error, print exceptions to console & to the web-page itself app.use(express.errorHandler({ dumpExceptions: true, showStack: true })); }); app.configure('production', function() { //On error, this simply says ‘Internal error Occurred’ app.use(express.errorHandler({ dumpExceptions: true, showStack: false })); app.use(express.logger()); //logs requests }); Things to Note: 1. Express uses NODE_ENV environment variable to set working environment 2. On CF, to toggle b/w ‘development’ and ‘production’, you can use.. vmc env-add <appName> NODE_ENV ‘production’ 49
  • 58. Hello World App using ExpressJS (Routing) var express = require('express'); Things to note: var app = express.createServer(); 1. You can use Routing to listen app.use(express.bodyParser()); to requests to call different functions //Receive HTTP GET app.get('/user', function(req, res) { 2. You can listen to HTTP POST, //Don’t let people call to /user PUT etc. throw new Error(“I’m private. Call me w/ a user id"); }); app.get('/user/:id', function(req, res){ res.send('user ' + req.params.id); }); //Receive HTTP POST app.post(‘’/register”, function(req, res) { //(Use bodyParser middleware for this) var body = req.body; db.save(body.user, body.password); } 50
  • 59. Hello World App using ExpressJS (Sessions) var express = require('express'); var app = express.createServer(); //Middlewares app.use(express.static(__dirname + ‘/public’)); //sets location of public files app.use(express.bodyParser()); //parses HTTP POST body app.use(express.cookieParser()); //Parses cookies headers app.use(express.session({secret: 'your secret here})); Things to note: 1. To create sessions, use cookieParser & session middlewares 2. By default Express uses MemoryStore to store sessions 3. You can use Redis to store sessions 51
  • 60. ExpressJS (Sticky sessions for multi instances) var express = require('express'); var app = express.createServer(); //Middlewares app.use(express.static(__dirname + ‘/public’)); //sets location of public files app.use(express.bodyParser()); //parses HTTP POST body app.use(express.cookieParser()); //Parses cookies headers app.use(express.session({secret: 'your secret here’, key: ‘jsessionid’ })); Things to note: 1. Sticky Session is a reverse proxy / load balancer feature to help persistent connection 2. When Sticky Session is on, request goes from Nginx to the same instance no matter how many instances of your app is running . 3. Cloud Foundry’s Nginx provides Sticky Sessions on ‘jsessionid’ cookie 4. W/o setting this requests are randomly sent to different instances & you’ll have to use external store like Redis to fetch session data (recommended). 52
  • 62. ExpressJS demo app screenshots (routing, sessions & sticky sessions) Demo Explains how session, sticky sessions, routing etc. works For more: https://github.com/rajaraodv/express1 54
  • 63. Socket.io on Cloud Foundry Socket.io 55
  • 64. Socket.io on Cloud Foundry (server side) var sio = require('socket.io'); var express = require('express'); var app = express.createServer(); var io = sio.listen(app);//listen to express io.configure(function() { io.set('log level', 1); io.set("transports", ["xhr-polling"]); //Currently CF doesn’t support websockets }); Things to Note: 1. Socket.io is mainly used to build real-time apps 2. Socket.io provides a single interface to switch b/w various transport techniques like xhr-polling, websocket, JSONP etc 3. In addition, it provides heartbeats, reconnection, timeouts etc. that are vital for real-time apps. 4. It works seamlessly with ExpressJS 56
  • 65. Socket.io on Cloud Foundry (server side continued) //After listening to express..wait for connection from browser io.sockets.on('connection', function(socket) { // When the client/browser emits 'sendchat', this listens and executes socket.on('sendchat', function(data) { // We will echo it back to ALL sockets io.sockets.emit('updatechat’, data); }); }); 57
  • 66. Socket.io on Cloud Foundry (client side) <script src="/socket.io/socket.io.js"></script>//socket.io serves this file from server var socket = io.connect(document.location.href); //connect to the server // on connection socket.on('connect', function() { console.log("client connected"); }); // Whenever the server emits 'updatechat', this updates the chat body socket.on('updatechat', function (data) { $('#conversation').append(data); // append it to my list }); //When the user enter some data, send it to server function sendchat() { var message = $('#chatField').val(); // Emit or tell server to execute 'sendchat’ socket.emit('sendchat', message); } 58
  • 67. Socket.io (+ ExpressJS) Demo app screenshots For more: https://github.com/rajaraodv/socketio1 59
  • 68. Simple coding and deployment - Cloud9 IDE 60
  • 69. Summary 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) • npm & package.json • Node.js EventEmitters 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 61
  • 70. Questions? Questions? @rajaraodv (github.com/rajaraodv) @andypiper (github.com/andypiper) @cloudfoundry (github.com/cloudfoundry) 62