Node.js: CAMTA Presentation

37
MUMPS is dead? Rob Tweed M/Gateway Developments Ltd http://www.mgateway.com Twitter: @rtweed

description

Presentation given at the CAMTA meeting, 1st December 2011

Transcript of Node.js: CAMTA Presentation

Page 1: Node.js: CAMTA Presentation

MUMPS is dead?Rob Tweed

M/Gateway Developments Ltd

http://www.mgateway.comTwitter: @rtweed

Page 2: Node.js: CAMTA Presentation

What is Node.js?

Page 3: Node.js: CAMTA Presentation

What is Node.js?

Page 4: Node.js: CAMTA Presentation

What is Node.js?

…so what does that mean?

Page 5: Node.js: CAMTA Presentation

What is Node.js?

Page 6: Node.js: CAMTA Presentation

What is Node.js?

• Node.js is a server-side JavaScript environment.

Page 7: Node.js: CAMTA Presentation

What is Node.js?

• Node.js is a server-side JavaScript environment.

• Node.js is a server-side JavaScript interpreter that allows us to handle and make requests via JavaScript:– In a web or networked environment

Page 8: Node.js: CAMTA Presentation

Just Javascript

Browsers

Node.jsserver

process

JavascriptJavascript Database

Page 9: Node.js: CAMTA Presentation

So instead of this:

Browser

Browser

Browser

WebServer

(Apache/IIS)

CSP Caché

http/https

CS

P interface

Caché

Persistentconnections

CS

P interface

Yourscripts

Yourdata

EWDCSP

pages

Page 10: Node.js: CAMTA Presentation

You'd have something like this:

Browser

Browser

Browser

Caché

http/https

WebLink interface

Caché

CS

P interface

Child_processconnections

EWDRuntime

Yourscripts

Yourdata

EWDroutines

Node.jsprocess

JavascriptWeb server &

request/responseHandling logic

stdin/stdout

GT.M orCaché

Page 11: Node.js: CAMTA Presentation

What is Node.js?

Page 12: Node.js: CAMTA Presentation

V8

• Node.js uses the Google V8 engine to provide the Javascript environment

• V8 is the engine in the Chrome browser

• V8 is very fast!

Page 13: Node.js: CAMTA Presentation

What is Node.js?

Page 14: Node.js: CAMTA Presentation

What is Node.js?

• Node.js is a server-side JavaScript environment that uses an asynchronous event-driven model.

Page 15: Node.js: CAMTA Presentation

What is Node.js?

• Node.js is a server-side JavaScript environment that uses an asynchronous event-driven model.

• No threads– Node.js runs in a single process, handling all client

requests

• Non-blocking I/O– Can't wait for slow resources to respond

• Files, databases, etc

– Must not block the process or everyone waits

Page 16: Node.js: CAMTA Presentation

Asynchronous I/O & Javascript

• Javascript is an ideal language for this

• No I/O in a browser

• Access to remote resources is asynchronous– The A in Ajax!

• Send a request for a resource and define a handler to deal with it when it arrives– "Call-back function"

Page 17: Node.js: CAMTA Presentation

Asynchronous I/O & Javascript

• On the server, in Node.js:– Make a request for I/O (file/ database)– Define a call-back function– Move on immediately

– When the I/O is complete:• Call-back function is fired to handle it

Page 18: Node.js: CAMTA Presentation

Asynchronous I/O & Javascript

Synchronous read from a file in MUMPS:

set file = "mydata.txt"open file:"r"use fileread buffer// do something with the buffer contents….// then move on to the next task

Page 19: Node.js: CAMTA Presentation

Asynchronous I/O & Javascript

Synchronous read from a file in MUMPS:

set file = "mydata.txt"open file:"r"use fileread buffer// do something with the buffer contents….// at this point the thread is blocked….// until the buffer contents are read from the file// then move on to the next task

Page 20: Node.js: CAMTA Presentation

Asynchronous I/O & Javascript

The Asynchronous way in Javascript:

var fs = require('fs');

fs.readFile('mydata.txt', function (err, buffer) { // Do something with the data in the buffer});

// Immediately execute the next statements// while it waits for I/O

Page 21: Node.js: CAMTA Presentation

What is Node.js?

Page 22: Node.js: CAMTA Presentation

Node.js Evented I/O

• Node.js is a single process running an event loop

• When an event occurs, fire a call-back function to handle it

• When no events are coming in, the Node.js process sits in a dormant state

Page 23: Node.js: CAMTA Presentation

Node.js process

• A Node.js process can handle many thousands of clients– Provided none of them block– Provided none of them do CPU-intensive

processing

Page 24: Node.js: CAMTA Presentation

Node.js / Javascript: popularity

• Node.js is now the most followed project on Github, exceeding Ruby on Rails

• Javascript is now the most popular language in the world, based on a number of measures including number of Github projects

Page 25: Node.js: CAMTA Presentation

Significance for GT.M & Caché

• Opportunity to use Javascript as a primary scripting language– Global storage for Javascript– JSON / Global mapping

Page 26: Node.js: CAMTA Presentation

Why Javascript?

• The world's most popular language

• The world's sexiest, coolest server-side environment

• Javascript's dynamic interpreted nature has many similarities to MUMPS

Page 27: Node.js: CAMTA Presentation

Why not MUMPS?

• Rightly or wrongly, the perception in the mainstream is that the MUMPS language is old-fashioned and inherently broken– Daily WTF article:

• http://thedailywtf.com/Articles/A_Case_of_the_MUMPS.aspx

• Yet global storage has a great deal to offer as a NoSQL database technology

• Result: the benefits and advantages of global storage are ignored because modern developers will refuse to learn the language

Page 28: Node.js: CAMTA Presentation

Making Global Storage Attractive

• Make it accessible via the coolest, sexiest, most popular language on the planet

• Demonstrate that global storage is a great NoSQL technology– "Universal NoSQL Engine"– http://www.mgateway.com/docs/universalNoSQL.pdf

Page 29: Node.js: CAMTA Presentation

Downside of Node.js

• Asynchronous programming– Callback hell!

• Deeply nested call-backs

– Complexity of logic• Essential to understand closures• 2 main types:

– Dependent– Concurrent

• Care needed when using for loops to invoke logic that includes callbacks

Page 30: Node.js: CAMTA Presentation

Dependent callbacks

myFunc1(inputs, function(error, results) { var value = results.value;

// myFunc2 can't run until myFunc1 returns its results

myFunc2(value, function(error, results) { var myResult = results.value; }); });

Page 31: Node.js: CAMTA Presentation

Concurrent callbacksvar count = 0;myFunc1(inputs, function(error, results) { count++; if (count === 3) myFunc4(inputs);});myFunc2(inputs, function(error, results) { count++; if (count === 3) myFunc4(inputs);});myFunc3(inputs, function(error, results) { count++; if (count === 3) myFunc4(inputs);});

// myFunc isn't invoked until all 3 functions are complete// Uses the properties of closures to allow count to be incremented by // the callbacks in the three functions

Page 32: Node.js: CAMTA Presentation

Closures

• Inner functions have access to the outer function's variables, even after the outer function has completed

Page 33: Node.js: CAMTA Presentation

Closures

• Inner functions have access to the outer function's variables, even after the outer function has completed

var digit_name = function() { var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];

return function(n) { return names[n]; };

};

var test = digit_name();// test is a function that takes a value

var text = test(3);console.log("text = " + text);

Page 34: Node.js: CAMTA Presentation

Closures

• Inner functions have access to the outer function's variables, even after the outer function has completed

var digit_name = function() { var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine'];

return function(n) { return names[n]; };

};

var test = digit_name();// test is a function that takes a value

var text = test(3);console.log("text = " + text);

Page 35: Node.js: CAMTA Presentation

Closures

• Could be rewritten to use a self-executing function as follows

var digit_name = function() {  var names = ['zero', 'one', 'two', 'three', 'four', 'five', 'six', 'seven',  'eight', 'nine'];  return function(n) {    return names[n];  };

}();

// digit_name is the result of running the outer function, // so it's a function that takes a value

var name = digit_name(3);

// name will be 'three'

Page 36: Node.js: CAMTA Presentation

Downside of Node.js

• Asynchronous programming– Database access functions can't return a

value• Value is embedded inside the callback• Can't chain database functions as objects:

– document.getElementById('x').parent.firstChild.tagName

Page 37: Node.js: CAMTA Presentation

Accessing globals from Node.js

• Cache & GT.M:– https://github.com/robtweed/ewdGateway– Complete webserver + gateway for EWD– Also includes global access functions, eg:

• get• set• getJSON• mFunction