52
Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Embed Size (px)

Citation preview

Page 1: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 1

Node.js - What?

EMEA PUG Challenge, November 2015, Copenhagen

6-Nov-15 Node.js - What?

Page 2: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 2

Agenda

Introduction Node.js

– What is it? Browsers Important modules Progress <-> Node.js

6-Nov-15 Node.js - What?

Page 3: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 3

Introduction

Robert Prediger

20 years experience in PROGRESS 15 years experience in web development 5 years experience in Node.js

[email protected]

6-Nov-15 Node.js - What?

Page 4: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 4

Node.js

Javascript is everywhere:– Browsers– Webservers– Databases– Mobile Devices

Adobe, Google, Apple and Microsoft are spending a huge amount of money!

JavaScript seems to be the worlds most used programming language.

6-Nov-15 Node.js - What?

Page 5: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 5

Node.js

What is Node.js– Server side Javascript– Built on Chrome V8 Engine– Event-driven, non blocking I/O model

What is it for?– Easily building fast, scalable network applications– Perfect for data-intensive real-time application that

run across distributed devices

6-Nov-15 Node.js - What?

Page 6: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 6

Browser

http://html5test.com/

6-Nov-15 Node.js - What?

Page 7: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 7

Browser

6-Nov-15 Node.js - What?

Page 8: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 8

Node.js

What is Node.js– Server side Javascript– Built on Chrome V8 Engine– Event-driven, non blocking I/O model

What is it for?– Easily building fast, scalable network applications– Perfect for data-intensive real-time application that

run across distributed devices

6-Nov-15 Node.js - What?

Page 9: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 9

Node.js

Blocking I/O

database, filesystem – disk S3, external APIs – networking

6-Nov-15 Node.js - What?

Page 10: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 10

Node.js

6-Nov-15 Node.js - What?

Page 11: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 11

Node.js

Request (other languages, incl. Progress):

6-Nov-15 Node.js - What?

Finished request

Start 2nd requestGreen is executing thread

Red is waiting on I/O

Page 12: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 12

Node.js

F-18 Hornet– Max speed of

1,190 mph RAM

Banana slug– Max speed of

0.007 mph Hard disk

6-Nov-15 Node.js - What?

Quelle: http://blog.scoutapp.com/articles/2011/02/10/understanding-disk-i-o-when-should-you-be-worried

Page 13: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 13

Node.js

6-Nov-15 Node.js - What?

Page 14: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 14

Nginx

6-Nov-15 Node.js - What?

Quelle: http://www.theorganicagency.com/apache-vs-nginx-performance-comparison/

Page 15: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 15

Nginx

6-Nov-15 Node.js - What?

Quelle: http://blog.webfaction.com/2008/12/a-little-holiday-present-10000-reqssec-with-nginx-2/

Page 16: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 16

Node.js

6-Nov-15 Node.js - What?

Page 17: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 17

Node.js

6-Nov-15 Node.js - What?

Q: How is it possible to handle parallel I/O with one thread of execution?

A: There is (usually) no such thing as parallel I/O

Page 18: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 18

Node.js

Parallel I/O: how we see it

6-Nov-15 Node.js - What?

Page 19: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 19

Node.js

Parallel I/O: what actually happen at low level

6-Nov-15 Node.js - What?

Page 20: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 20

Node.js

6-Nov-15 Node.js - What?

OS does a good job of abstracting sequential stream of raw data into logical streams: tcp sockets, http connections

Page 21: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 21

Node.js

6-Nov-15 Node.js - What?

„everything runs in parallel ...

... except your code“

- it seems that a lot of people are confused by this phrase which tries to explain node.js asynchronous nature

Page 22: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 22

Node.js

6-Nov-15 Node.js - What?

“everything” = I/O

Since I/O is external to the code, I’d prefer to say “In node, code is organised to wait for as much as possible in parallel”

Page 23: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 23

Node.js

6-Nov-15 Node.js - What?

We are not “running” functions in parallel. We are waiting for data “in parallel”.

One WorldOne WAIT-FOR

Page 24: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 24

Node.js

Request with WebSpeed:

6-Nov-15 Node.js - What?

Finished request

Start 2nd requestGreen is executing thread

Red is waiting on I/O

Page 25: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 25

Node.js

6-Nov-15 Node.js - What?

Request with Node:

Green is executing thread

Red is waiting on I/O

Event Loop

Request

Request

Page 26: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 26

Node.js

6-Nov-15 Node.js - What?

Page 27: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 27

Node.js

Can handle thousands of concurrent connectionswith minimal overhead (CPU/Memory)

on a single Process!

6-Nov-15 Node.js - What?

Page 28: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 28

Node.js

Summary Extremely efficient networking applications Fast javascript runtime (V8) Rapid growth in both, packages and community

6-Nov-15 Node.js - What?

Page 29: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 29

Module

6-Nov-15 Node.js - What?

Page 30: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 30

Modules

Number of Node.js modules exceeded 200.000! Modules for nearly every problem:

– pm2– cluster– express– socket.io– restify– async– q (promises)– debug– winston

6-Nov-15 Node.js - What?

Page 31: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 31

PM2 is a production process manager for Node.js applications with a built-in load

balancer.

It allows you to keep applications alive forever, to reload them without downtime and to facilitate

common system admin tasks.

http://pm2.keymetrics.io/

Node.js - What?

pm2

6-Nov-15

Page 32: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 32

cluster

6-Nov-15 Node.js - What?

var cluster = require('cluster'), http = require('http‘);

if (cluster.isMaster) { // Fork workers. for (var i = 0; i < numCPUs; i++) { cluster.fork(); }

cluster.on('exit', function(worker, code, signal) { console.log('worker ' + worker.process.pid + ' died'); });

} else { // Workers can share any TCP connection // In this case its a HTTP server http.createServer(function(req, res) { res.writeHead(200); res.end("hello world\n"); }).listen(8000);}

Page 33: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 33

express

http://expressjs.com/

6-Nov-15 Node.js - What?

var express = require('express'), app = express();

app.configure(function(){ app.use( express.favicon() ); app.use( express.bodyParser() ); app.use( express.static( './public') );});

app.get( '/login‘, route.get );

app.listen( 8081 );

Page 34: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 34

socket.io

Protocol for having a bidirectional

communicationwith client.

6-Nov-15 Node.js - What?

Page 35: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 35

socket.io

http://socket.io/

6-Nov-15 Node.js - What?

var io  = require('socket.io').listen( 80 );

io.sockets.on('connection', function (socket) { socket.emit( 'message', { text: 'Hello World' }); // broadcast message to all subscribed sockets socket.broadcast.emit("chat", { name: socket.nickname, msg: msg });});

Page 36: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 36

socket.io

Demo

6-Nov-15 Node.js - What?

Page 37: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 37

restify - Client

https://github.com/mcavage/node-restify

6-Nov-15 Node.js - What?

var client = restify.createJsonClient({ url: 'https://api.us-west-1.joyentcloud.com', version: '*‘ });

client.post('/foo', { hello: 'world' }, function(err, req, res, obj) { assert.ifError(err); console.log( res.statusCode, '->', res.headers ); console.log( obj );});

Page 38: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 38

restify - Server

https://github.com/mcavage/node-restify

6-Nov-15 Node.js - What?

var restify = require('restify‘), server = restify.createServer({ name: 'myapp', version: '1.0.0' });

server.use(restify.acceptParser(server.acceptable));server.use(restify.queryParser());server.use(restify.bodyParser());server.get('/echo/:name', function (req, res, next ) {

res.send(req.params);return next();

});

server.listen( 8080, function () { console.log('%s listening at %s', server.name,

server.url); });

Page 39: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 39

async

https://github.com/caolan/async

6-Nov-15 Node.js - What?

// an example using an object instead of an array async.parallel({

one: function(callback){ setTimeout(function(){

callback(null, 1); }, 200);

},two: function(callback){

setTimeout(function(){callback(null, 2);

}, 100); }},function(err, results) {

// results is now equals to: {one: 1, two: 2}}

);

Page 40: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 40Node.js - What?

debug

https://github.com/visionmedia/debug

6-Nov-15

var debug = require("debug"), log = debug("demo:socket");

function login ( name ) { log( "login", name ); client.nickname = name;}

Page 41: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 41Node.js - What?

winston

https://github.com/flatiron/winston

6-Nov-15

var winston = require('winston');

var logger = new (winston.Logger)( { transports: [ new (winston.transports.Console)( { level: 'warn' } ), new (winston.transports.File)( {

filename: 'somefile.log', level: 'error‘ } )

]});

logger.debug( "Will not be logged in either transport!" );logger.transports.console.level = 'debug';logger.transports.file.level = 'verbose';logger.verbose("Will be logged in both transports!");

Page 42: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 42

Progress - WebSpeed

REST: Node.js -> Progress WebSpeed

– pro• Easy to maintain

– con• Overhead with HTTP• Messenger

6-Nov-15 Node.js - What?

Page 43: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 43

Progress - WebSpeed

6-Nov-15 Node.js - What?

{src/web/method/cgidefs.i}output-content-type("application/json":U).

IF NOT WEB-CONTEXT:IS-JSON THEN DO: LOG-MANAGER:WRITE-MESSAGE( "No JSON request" ). RETURN.END.

/* read json */oParse = NEW ObjectModelParser().jData = CAST( oParse:Parse( WEB-CONTEXT:HANDLE ), JsonObject ).

Page 44: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 44

Progress - WebSpeed

6-Nov-15 Node.js - What?

...

hDataSet:FILL().

jResult = NEW JsonObject().jDataSet = NEW JsonObject().

jDataSet:READ( hDataSet ).jResult:Add( "data", jDataSet ).

jResult:WriteStream( "Webstream" ).

Page 45: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 45

Progress - AppServer

REST: Node.js -> Progress AppServer

– pro• Webservice

– con• Overhead with Tomcat• Deployment with ProxyGen

6-Nov-15 Node.js - What?

Page 46: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 46

Progress – node4progress

node4progress– https://github.com/FrankHilhorst/node4progress

– pro• very fast• Structured API

– con• Overhead with java engine between

6-Nov-15 Node.js - What?

Page 47: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 47

Progress – node4progress

6-Nov-15 Node.js - What?

var conf = require("./config.json");var n4p = require("node4progress")(conf);

n4p.setAppsvrProc("CustomerHandler.p","",false,true);n4p.setParameter("InputPars","longchar","input","batchNum=2","");n4p.setParameter("OutputPars","character","output","","");n4p.setParameter("dsCustomer","dataset-handle","output","","");n4p.setParameter("ErrMsg","character","output","","");

n4p.appProc().execute(function(err,result){ if(err){ console.log("ERROR->"+err); }else{ result=JSON.parse(result); }});

Page 48: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 48

Progress – node4progress

6-Nov-15 Node.js - What?

var conf = require('config.json'), n4p = require('node4progressHttp')conf.prgs);

var handler = "handlers/CustomerHandler.p", inputPars = "NumCustomersToPull=2";

n4p.callHandler( handler, inputPars, function(result){ console.log(result);});

Page 49: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 49

Progress – JSDO

JSDO– https://github.com/CloudDataObject/JSDO– https://documentation.progress.com/output/pdo/

– pro• very fast• Structured API

– con• Infrastructure (beginning with OE 11.5)

6-Nov-15 Node.js - What?

Page 50: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 50

Progress – JSDO

6-Nov-15 Node.js - What?

XMLHttpRequest = require("./XMLHttpRequest.js").XMLHttpRequest;Require("./progress.js"); Require("./progress.session.js");

// get connection to progress and get catalogvar session = new progress.data.Session();session.login( conf.serviceURI, "", "");session.addCatalog( conf.catalogURI );

Page 51: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 51

Progress – JSDO

6-Nov-15 Node.js - What?

// create jsdovar jsdo = new progress.data.JSDO( { name: 'dsCustomer' } );jsdo.subscribe( 'AfterFill', onAfterFillCustomers, this);jsdo.fill(); // fills the locally initialized jsdo from the catalog

function onAfterFillCustomers( jsdo, success, request ) { var result = []; jsdo.eCustomer.foreach( function( cust ) { result.push( cust.data ); }); callback( null, result );}

Page 52: Page 1 Node.js - What? EMEA PUG Challenge, November 2015, Copenhagen 6-Nov-15 Node.js - What?

Page 52

End

Questions?

[email protected]

6-Nov-15 Node.js - What?