Node.js: CAMTA Presentation
-
Upload
rob-tweed -
Category
Technology
-
view
2.765 -
download
0
description
Transcript of Node.js: CAMTA Presentation
MUMPS is dead?Rob Tweed
M/Gateway Developments Ltd
http://www.mgateway.comTwitter: @rtweed
What is Node.js?
What is Node.js?
What is Node.js?
…so what does that mean?
What is Node.js?
What is Node.js?
• Node.js is a server-side JavaScript environment.
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
Just Javascript
Browsers
Node.jsserver
process
JavascriptJavascript Database
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
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é
What is Node.js?
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!
What is Node.js?
What is Node.js?
• Node.js is a server-side JavaScript environment that uses an asynchronous event-driven model.
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
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"
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
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
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
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
What is Node.js?
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
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
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
Significance for GT.M & Caché
• Opportunity to use Javascript as a primary scripting language– Global storage for Javascript– JSON / Global mapping
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
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
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
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
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; }); });
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
Closures
• Inner functions have access to the outer function's variables, even after the outer function has completed
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);
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);
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'
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
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