153
APEX, Node.js and HTML5: Magic! Alan Arentsen Alex Nuijten

APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

  • Upload
    others

  • View
    6

  • Download
    0

Embed Size (px)

Citation preview

Page 1: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

APEX, Node.js and HTML5: Magic!

Alan ArentsenAlex Nuijten

Page 2: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

alanarentsen.blogspot.com

@alanarentsen

Page 3: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

nuijten.blogspot.com

@alexnuijten

Page 4: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 5: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 6: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Tim Berners-Lee

1990 1995HTML 2.0

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">

Page 7: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

1997HTML 3.2

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">

Page 8: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

1997HTML 4.0

Page 9: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

1999HTML 4.0.1

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN” "http://www.w3.org/TR/html4/strict.dtd">

Page 10: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

2000XHTML 1.0

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

Page 11: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

2001XHTML 1.1

Page 12: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

2008HTML 5

Working Draft

Page 13: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

2009XHTML 2.0✝

Page 14: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

2012HTML 5

Candidate Recommendation

Page 15: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

<!DOCTYPE html>

2014HTML 5

Recommendation

28 October

Page 16: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

2015HTML 5.1

Candidate Recommendation

Page 17: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Input Types Graphics

VideoAudio

Geolocation

DragDropLocal Storage

Web WorkersWeb Sockets

Page 18: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Web Sockets

bi-directional, full-duplex communication.

Draft status

Page 19: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

APEX, Node.js and HTML5: Magic!

Alan ArentsenAlex Nuijten

Websockets

Page 20: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Poll vs. Push

Page 21: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Background Process

NoAre you done yet?

No

Are you done yet?

No

Are you done yet?

NoAre you done yet?No

Are you done yet?

NoAre you done yet?

Page 22: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Background Process

I am Done!

Page 23: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Physical

Data

Application

Presentation

Session

Transport

Network

HTTP

Websockets

Page 24: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

My First Websocket

Page 25: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

http://www.websocket.org/echo.html

Page 26: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 27: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 28: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 29: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 30: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

$(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { console.log ('There is no websocket API available.'); } else { ws_connection = new WebSocket ('wss://echo.websocket.org'); ws_connection.onmessage = function(message){ console.log ('Echo: ' + message.data); alert (message.data); } } });

var ws_connection;

Page 31: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

$(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { console.log ('There is no websocket API available.'); } else { ws_connection = new WebSocket ('wss://echo.websocket.org'); ws_connection.onmessage = function(message){ console.log ('Echo: ' + message.data); alert (message.data); } } });

var ws_connection;

Page 32: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

$(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { console.log ('There is no websocket API available.'); } else { ws_connection = new WebSocket ('wss://echo.websocket.org'); ws_connection.onmessage = function(message){ console.log ('Echo: ' + message.data); alert (message.data); } } });

var ws_connection;

Page 33: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

$(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { console.log ('There is no websocket API available.'); } else { ws_connection = new WebSocket ('wss://echo.websocket.org'); ws_connection.onmessage = function(message){ console.log ('Echo: ' + message.data); alert (message.data); } } });

var ws_connection;

Page 34: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

$(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { console.log ('There is no websocket API available.'); } else { ws_connection = new WebSocket ('wss://echo.websocket.org'); ws_connection.onmessage = function(message){ console.log ('Echo: ' + message.data); alert (message.data); } } });

var ws_connection;

Page 35: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

$(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { console.log ('There is no websocket API available.'); } else { ws_connection = new WebSocket ('wss://echo.websocket.org'); ws_connection.onmessage = function(message){ console.log ('Echo: ' + message.data); alert (message.data); } } });

var ws_connection;

Page 36: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 37: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.send (apex.item("P1_ECHO_TEXT").getValue());

Page 38: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 39: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 40: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

$(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { console.log ('There is no websocket API available.'); } else { ws_connection = new WebSocket ('wss://echo.websocket.org'); ws_connection.onmessage = function(message){ console.log ('Echo: ' + message.data); alert (message.data); } } });

var ws_connection;

Page 41: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 42: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 43: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 44: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 45: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect for

data-intensive real-time applications that run across distributed devices.“ ”

Page 46: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, perfect

for data-intensive real-time applications that run across distributed devices.

Page 47: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

JSJavaScript on the Server

Page 48: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 49: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

websockethttp

oraclerest

https

pythongrunt

filbert

soap-q

q-superagent

mongodb-q

underscoresocket.io

gulp

expressmocha

async

lodash

mongoose

hapi

commander

browserify

redis

jade

colorsmoment

coffee-scriptpassportconnect

nodemailer

q

bower

cheerio

chalk

sails

optimist

mysqluglify-js

jshint

nodemon

stylus

forever

mkdir

validator

debug

less

bluebird gm awesome

Page 50: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

websockethttp

oraclerest

https

pythongrunt

filbert

soap-q

q-superagent

mongodb-q

underscoresocket.io

gulp

expressmocha

async

lodash

mongoose

hapi

commander

browserify

redis

jade

colorsmoment

coffee-scriptpassportconnect

nodemailer

q

bower

cheerio

chalk

sails

optimist

mysqluglify-js

jshint

nodemon

stylus

forever

mkdir

validator

debug

less

bluebird gm awesome

Page 51: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

My First Node.js

Page 52: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 53: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'Basic NodeJS Echo server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 54: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'Basic NodeJS Echo server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Check Requirements

Page 55: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'Basic NodeJS Echo server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 56: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'Basic NodeJS Echo server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 57: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* * Events on WebSocket server */ wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); console.log((new Date()) + ' Connection accepted from ' + request.origin);

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = message.utf8Data.toLowerCase(); connection.sendUTF(msgString); }); connection.on('close', function(connection) { console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 58: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* * Events on WebSocket server */ wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); console.log((new Date()) + ' Connection accepted from ' + request.origin);

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = message.utf8Data; connection.sendUTF(msgString); }); connection.on('close', function(connection) { console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 59: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* * Events on WebSocket server */ wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); console.log((new Date()) + ' Connection accepted from ' + request.origin);

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = message.utf8Data; connection.sendUTF(msgString); }); connection.on('close', function(connection) { console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 60: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* * Events on WebSocket server */ wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); console.log((new Date()) + ' Connection accepted from ' + request.origin);

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = message.utf8Data; connection.sendUTF(msgString); }); connection.on('close', function(connection) { console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 61: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

$(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { console.log('There is no websocket API available.'); } else { ws_connection = new WebSocket ('ws://apex.ordina.nl:4880', 'echo-protocol'); ws_connection.onmessage = function(message){ alert (message.data); } } });

Page 62: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

$(function() { window.WebSocket = window.WebSocket || window.MozWebSocket; if (!window.WebSocket) { console.log('There is no websocket API available.'); } else { ws_connection = new WebSocket ('ws://apex.ordina.nl:4880', 'echo-protocol'); ws_connection.onmessage = function(message){ alert (message.data); } } });

Page 63: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.send (apex.item("P1_ECHO_TEXT").getValue());

Page 64: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 65: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 66: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Chat Application

Page 67: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Hi Everybody!

Page 68: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'Broadcast NodeJS Chat server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 69: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'Broadcast NodeJS Chat server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 70: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'Broadcast NodeJS Chat server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 71: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'Broadcast NodeJS Chat server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 72: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'Broadcast NodeJS Chat server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 73: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var clients = [ ]; wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); var index = clients.push(connection) - 1;

console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

Page 74: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var clients = [ ]; wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); var index = clients.push(connection) - 1;

console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

Page 75: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var clients = [ ]; wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); var index = clients.push(connection) - 1;

console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

Page 76: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var clients = [ ]; wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); var index = clients.push(connection) - 1;

console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

Page 77: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = message.utf8Data; for(var i in clients){ clients[i].sendUTF(msgString); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }); connection.on('close', function(connection) { clients.splice(index, 1); console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 78: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = message.utf8Data; for(var i in clients){ clients[i].sendUTF(msgString); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }); connection.on('close', function(connection) { clients.splice(index, 1); console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 79: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = message.utf8Data; for(var i in clients){ clients[i].sendUTF(msgString); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }); connection.on('close', function(connection) { clients.splice(index, 1); console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 80: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = message.utf8Data; for(var i in clients){ clients[i].sendUTF(msgString); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }); connection.on('close', function(connection) { clients.splice(index, 1); console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 81: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = message.utf8Data; for(var i in clients){ clients[i].sendUTF(msgString); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }); connection.on('close', function(connection) { clients.splice(index, 1); console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 82: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Hi Everybody

Hi Everybody

Hi Everybody

Hi Everybody

Hi Everybody

Hi Everybody

Page 83: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Hello there!Hello there!

Page 84: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Send to

Message

Who is Connected?

Page 85: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Task

Payload

Chat MessageReport Connected Users

Complete MessageAll Connected Users

Page 86: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

{task : 'the activity' ,payload : could be anything }

Page 87: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

{task : 'chat_message' ,payload : {to_key : 'ACB123DSFJH123' ,msg : 'Hello There'} }

Page 88: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

{task : 'connected_users' ,payload : [{'key' : 'ACB123DSFJH123' ,'username' : 'Arthur Dent'} ,{'key' : '4321CBAFGH4513' ,'username' : 'Ford Prefect'} ,{'key' : '56AFGHK90UH871' ,'username' : 'Marvin the Paranoid Android' } ] }}

Page 89: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 90: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

key = apex.item('P4_CHATMATES').getValue(); message = apex.item('P4_TEXTMESSAGE').getValue(); jsonMsg = JSON.stringify( {task : 'chat_message' ,payload : {to_key : key ,msg : message} }); ws_connection.send (jsonMsg);

Send

Page 91: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

key = apex.item('P4_CHATMATES').getValue(); message = apex.item('P4_TEXTMESSAGE').getValue(); jsonMsg = JSON.stringify( {task : 'chat_message' ,payload : {to_key : key ,msg : message} }); ws_connection.send (jsonMsg);

Send

Page 92: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

key = apex.item('P4_CHATMATES').getValue(); message = apex.item('P4_TEXTMESSAGE').getValue(); jsonMsg = JSON.stringify( {task : 'chat_message' ,payload : {to_key : key ,msg : message} }); ws_connection.send (jsonMsg);

Send

Page 93: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

key = apex.item('P4_CHATMATES').getValue(); message = apex.item('P4_TEXTMESSAGE').getValue(); jsonMsg = JSON.stringify( {task : 'chat_message' ,payload : {to_key : key ,msg : message} }); ws_connection.send (jsonMsg);

Send

Page 94: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

key = apex.item('P4_CHATMATES').getValue(); message = apex.item('P4_TEXTMESSAGE').getValue(); jsonMsg = JSON.stringify( {task : 'chat_message' ,payload : {to_key : key ,msg : message} }); ws_connection.send (jsonMsg);

Send

Page 95: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'user_broadcast') { var jsonResult = jsonMsg.payload;

$('#P4_CHATMATES').find('option').remove().end(); $.each(jsonResult, function() { $('#P4_CHATMATES').append( $("<option></option>") .text(this.username) .val(this.key) ); }); } else if (jsonMsg.task == 'chat_message') { apex.item('P4_CHATBOX').setValue(jsonMsg.payload.msg); } };

Receive

Page 96: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'user_broadcast') { var jsonResult = jsonMsg.payload;

$('#P4_CHATMATES').find('option').remove().end(); $.each(jsonResult, function() { $('#P4_CHATMATES').append( $("<option></option>") .text(this.username) .val(this.key) ); }); } else if (jsonMsg.task == 'chat_message') { apex.item('P4_CHATBOX').setValue(jsonMsg.payload.msg); } };

Receive

Page 97: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'user_broadcast') { var jsonResult = jsonMsg.payload;

$('#P4_CHATMATES').find('option').remove().end(); $.each(jsonResult, function() { $('#P4_CHATMATES').append( $("<option></option>") .text(this.username) .val(this.key) ); }); } else if (jsonMsg.task == 'chat_message') { apex.item('P4_CHATBOX').setValue(jsonMsg.payload.msg); } };

Receive

Page 98: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'user_broadcast') { var jsonResult = jsonMsg.payload;

$('#P4_CHATMATES').find('option').remove().end(); $.each(jsonResult, function() { $('#P4_CHATMATES').append( $("<option></option>") .text(this.username) .val(this.key) ); }); } else if (jsonMsg.task == 'chat_message') { apex.item('P4_CHATBOX').setValue(jsonMsg.payload.msg); } };

Receive

Page 99: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'user_broadcast') { var jsonResult = jsonMsg.payload;

$('#P4_CHATMATES').find('option').remove().end(); $.each(jsonResult, function() { $('#P4_CHATMATES').append( $("<option></option>") .text(this.username) .val(this.key) ); }); } else if (jsonMsg.task == 'chat_message') { apex.item('P4_CHATBOX').setValue(jsonMsg.payload.msg); } };

Receive

Page 100: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'user_broadcast') { var jsonResult = jsonMsg.payload;

$('#P4_CHATMATES').find('option').remove().end(); $.each(jsonResult, function() { $('#P4_CHATMATES').append( $("<option></option>") .text(this.username) .val(this.key) ); }); } else if (jsonMsg.task == 'chat_message') { apex.item('P4_CHATBOX').setValue(jsonMsg.payload.msg); } };

Receive

Page 101: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'One on One NodeJS Chat server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 102: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

"use strict";

process.title = 'One on One NodeJS Chat server';

var http = require('http'); var webSocketServer = require('websocket').server;

/* * HTTP server */ var server = http.createServer(function(req, resp){}); server.listen(4880, function() { console.log((new Date()) + " HTTP server is listening on port 4880"); });

/* * WebSocket server */ var wsServer = new webSocketServer({ httpServer: server });

Page 103: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var clients = [ ]; wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); connection.key = request.key; connection.username = request.resource.substring(1);

var index = clients.push(connection) - 1; console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

userBroadcast();

Page 104: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var clients = [ ]; wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); connection.key = request.key; connection.username = request.resource.substring(1);

var index = clients.push(connection) - 1; console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

userBroadcast();

Page 105: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var clients = [ ]; wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); connection.key = request.key; connection.username = request.resource.substring(1);

var index = clients.push(connection) - 1; console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

userBroadcast();

Page 106: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var clients = [ ]; wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); connection.key = request.key; connection.username = request.resource.substring(1);

var index = clients.push(connection) - 1; console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

userBroadcast();

Page 107: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var clients = [ ]; wsServer.on('request', function(request) { console.log((new Date()) + ' Websocketconnection from origin ' + request.origin + '.'); var connection = request.accept('echo-protocol', request.origin); connection.key = request.key; connection.username = request.resource.substring(1);

var index = clients.push(connection) - 1; console.log((new Date()) + ' Connection accepted from ' + request.origin + '.');

userBroadcast(); More on this function

in a little bit

Page 108: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = JSON.parse(message.utf8Data); if (msgString.task == 'chat_message') { for(var i in clients){ if (clients[i].key == msgString.payload.to_key) { msgStringOut = JSON.stringify( {task :'chat_message' ,payload:{from_key:connection.key ,msg :msgString.payload.msg} });

clients[i].sendUTF(msgStringOut); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }; }; });

Page 109: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = JSON.parse(message.utf8Data); if (msgString.task == 'chat_message') { for(var i in clients){ if (clients[i].key == msgString.payload.to_key) { msgStringOut = JSON.stringify( {task :'chat_message' ,payload:{from_key:connection.key ,msg :msgString.payload.msg} });

clients[i].sendUTF(msgStringOut); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }; }; });

Page 110: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = JSON.parse(message.utf8Data); if (msgString.task == 'chat_message') { for(var i in clients){ if (clients[i].key == msgString.payload.to_key) { msgStringOut = JSON.stringify( {task :'chat_message' ,payload:{from_key:connection.key ,msg :msgString.payload.msg} });

clients[i].sendUTF(msgStringOut); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }; }; });

Page 111: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = JSON.parse(message.utf8Data); if (msgString.task == 'chat_message') { for(var i in clients){ if (clients[i].key == msgString.payload.to_key) { msgStringOut = JSON.stringify( {task :'chat_message' ,payload:{from_key:connection.key ,msg :msgString.payload.msg} });

clients[i].sendUTF(msgStringOut); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }; }; });

Page 112: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = JSON.parse(message.utf8Data); if (msgString.task == 'chat_message') { for(var i in clients){ if (clients[i].key == msgString.payload.to_key) { msgStringOut = JSON.stringify( {task :'chat_message' ,payload:{from_key:connection.key ,msg :msgString.payload.msg} });

clients[i].sendUTF(msgStringOut); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }; }; });

Page 113: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = JSON.parse(message.utf8Data); if (msgString.task == 'chat_message') { for(var i in clients){ if (clients[i].key == msgString.payload.to_key) { msgStringOut = JSON.stringify( {task :'chat_message' ,payload:{from_key:connection.key ,msg :msgString.payload.msg} });

clients[i].sendUTF(msgStringOut); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }; }; });

Page 114: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

/* Events on websocket connection */ connection.on('message', function(message) { var msgString = JSON.parse(message.utf8Data); if (msgString.task == 'chat_message') { for(var i in clients){ if (clients[i].key == msgString.payload.to_key) { msgStringOut = JSON.stringify( {task :'chat_message' ,payload:{from_key:connection.key ,msg :msgString.payload.msg} });

clients[i].sendUTF(msgStringOut); console.log((new Date()) + ' message sent to: ' + connection.remoteAddress + '.'); }; }; }; });

Page 115: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

connection.on('close', function(connection) { clients.splice(index, 1); userBroadcast(); console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 116: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

connection.on('close', function(connection) { clients.splice(index, 1); userBroadcast(); console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 117: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

connection.on('close', function(connection) { clients.splice(index, 1); userBroadcast(); console.log((new Date()) + ' Peer ' + connection.remoteAddress + ' disconnected.'); }); });

Page 118: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var userBroadcast = function() { var usernames = [] for(var i in clients){ usernames.push( {username: clients[i].username ,key : clients[i].key}); }; for(var i in clients){ clients[i].sendUTF( JSON.stringify( {task :'user_broadcast' ,payload:usernames})); }; };

Here’s the userBroadcast

function

Page 119: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var userBroadcast = function() { var usernames = [] for(var i in clients){ usernames.push( {username: clients[i].username ,key : clients[i].key}); }; for(var i in clients){ clients[i].sendUTF( JSON.stringify( {task :'user_broadcast' ,payload:usernames})); }; };

Page 120: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

var userBroadcast = function() { var usernames = [] for(var i in clients){ usernames.push( {username: clients[i].username ,key : clients[i].key}); }; for(var i in clients){ clients[i].sendUTF( JSON.stringify( {task :'user_broadcast' ,payload:usernames})); }; };

Page 121: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Hello Alex! Hello Alex!

Page 122: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Hi Alan!Hi Alan!

Page 123: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

mobile phone push alerttodo list

alerts from background processfile ready on server

database alert fileorder which needs to be approved

tasklistsappointment

Page 124: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Communicate with Things

Page 125: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

“Oh my Gosh! I love this song”

Page 126: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 127: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 128: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

move

Page 129: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Lego Mindstorms

Page 130: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 131: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Brick

Page 132: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Motors

Page 133: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Motors

Page 134: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Light Sensor

Page 135: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Touch Sensor

Page 136: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 137: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 138: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 139: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Task

Payload

Pick Order

Order Contents

Page 140: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

jsonMsg = JSON.stringify( {task : 'pick_orders' ,payload : {'Red' : '1' ,'Green' : '1' } } ); ws_connection.send (jsonMsg);

Send

Page 141: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

jsonMsg = JSON.stringify( {task : 'pick_orders' ,payload : {'Red' : '1' ,'Green' : '1' } } ); ws_connection.send (jsonMsg);

Send

Page 142: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

jsonMsg = JSON.stringify( {task : 'pick_orders' ,payload : {'Red' : '1' ,'Green' : '1' } } ); ws_connection.send (jsonMsg);

Send

Page 143: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'pick_orders_started') { console.log ('Picking Process Started'); } else if (jsonMsg.task == 'orders_picked') { console.log ('Order Completed'); } };

Receive

Page 144: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'pick_orders_started') { console.log ('Picking Process Started'); } else if (jsonMsg.task == 'orders_picked') { console.log ('Order Completed'); } };

Receive

Page 145: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'pick_orders_started') { console.log ('Picking Process Started'); } else if (jsonMsg.task == 'orders_picked') { console.log ('Order Completed'); } };

Receive

Page 146: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

ws_connection.onmessage = function(message) { var jsonMsg = JSON.parse(message.data); if (jsonMsg.task == 'pick_orders_started') { console.log ('Picking Process Started'); } else if (jsonMsg.task == 'orders_picked') { console.log ('Order Completed'); } };

Receive

Page 147: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Track

Lightsensor Pushrod

Collection Box

Page 148: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Track

Lightsensor

Pushrod

Brick

Emergency BrakeCollection Box

Page 149: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Order:1 x Red 1 x Green

Page 150: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0
Page 151: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Alan [email protected]

alanarentsen.blogspot.com

Alex [email protected]

nuijten.blogspot.com

Page 152: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0

Questions and Answers

Page 153: APEX, Node.js and HTML5: Magic! fileTim Berners-Lee 1990 1995 HTML 2.0