Event-driven IO server-side JavaScript environment based on V8 Engine

36
Ricardo Silva Degree in Computer Science @ ISTEC MSc Computation and Medical Instrumentation @ ISEP Software Developer @ Shortcut, Lda (blog): http://blog.ricardosilva.pt (email): [email protected]

description

Event-driven IO server-side JavaScript environment based on V8 Engine

Transcript of Event-driven IO server-side JavaScript environment based on V8 Engine

Page 1: Event-driven IO server-side JavaScript environment based on V8 Engine

Ricardo Silva

Degree in Computer Science @ ISTECMSc Computation and Medical Instrumentation @ ISEP

Software Developer @ Shortcut, Lda

(blog): http://blog.ricardosilva.pt

(email): [email protected]

Page 2: Event-driven IO server-side JavaScript environment based on V8 Engine

Event-driven I/O server-side JavaScript environment based

on V8 Engine

Page 3: Event-driven IO server-side JavaScript environment based on V8 Engine

JavaScript(examples)

Page 4: Event-driven IO server-side JavaScript environment based on V8 Engine

JavaScript: Array vs ObjectInstantiation

• Arrayvar ar = []; /* ou new Array(); */

• Objectvar carColors = {}; /* ou new Object();*/

Page 5: Event-driven IO server-side JavaScript environment based on V8 Engine

JavaScript: Array vs ObjectAdd

• Arrayvar ar = []; ar[0] = 'January';Ou ar.push(‘March’);

• Objectvar carColors = {};carColors['BMW'] = 'Red';carColors['Audi'] = 'Blue';

Page 6: Event-driven IO server-side JavaScript environment based on V8 Engine

JavaScript: Array vs ObjectRemove

• Arrayar.splice(0,1);

• Objectdelete carColors['BMW'];

Page 7: Event-driven IO server-side JavaScript environment based on V8 Engine

JavaScript: Array vs ObjectLookup

• Arrayvar ar = [];for (var i = 0; i < carColors.length; i++) { alert( ar[i] );};Ouvar month = ar[0];

• Objectvar carColors = {};for (var i in carColors) { alert( carColors[i] );};Ouvar colorOfBWM = carColors[‘BMW’];

Page 8: Event-driven IO server-side JavaScript environment based on V8 Engine

JavaScript: callback Functions

var state;

function setstate (pbool, difCallBack) {if (state != pbool)

return (state = pbool), difCallBack(state);}

setstate(true, function(s){alert(s);

});

“We can keep on doing other things while waiting for the callback to be called.”

Page 9: Event-driven IO server-side JavaScript environment based on V8 Engine

JavaScript: Events

var eventHandlers = new Object(); this.registerEvent=function(name,callback){ var eh=(eventHandlers[name])?eventHandlers[name]:(eventHandlers[name]=[]); eh[eh.length]=callback; }; this.unregisterEvent=function(name,callback){ var eh=eventHandlers[name]; if("object"==typeof eh) for(var h in eh) if(eh[h]===callback){ delete eh[h]; return; } };

Page 10: Event-driven IO server-side JavaScript environment based on V8 Engine

JavaScript: Events

this.triggerEvent=function(name,args){ var eh=eventHandlers[name]; if("object"==typeof eh) for(var h in eh) eh[h](args); };

this.ring=function(evDelegate){ xpto.registerEvent('ring',evDelegate); };

function onRing(args){ xpto.triggerEvent('ring',args); }

Page 11: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js

Page 12: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js: What is?

Node.js is a platform built on Chrome's JavaScript runtime for easily building fast, scalable network applications. 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 13: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js: Companies using node!

• LinkedIn(Mobile WebApp)• Ebay(Data retrieval gateway)• GitHub(for Downloads)• Palm/HP(in WebOS)

• Yahoo!Mail• Rackspace(Cloud kick monitoring)• Voxxer(Push to Talk mobile app)

Page 14: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js: How to install?

• $ git clone \ git://github.com/joyent/node.git• $ cd node• $ git checkout v0.6.0• $ ./configure• $ sudo make install

(windows users: download node.exe)

Page 15: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.jsDEMO

Page 16: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js

“ Come on…, server side JS has been

around since 1996 …”

Page 17: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js

“ …what is so special about node?

Page 18: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js: Seep

Speed• Node can do ~6000 http requests / sec per CPU core• It is no problem to handle thousands of concurrent

connections which are moderately active

Page 19: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js: V8 JS Engine

V8 JavaScript Engine• Developed by Google in Aarhus, Denmark for Google

Chrome• Translates JavaScript in Assembly Code• Crankshaft JIT (enabled in 0.6.0 by default)

Page 20: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js: Non-Blocking I/O

Non-Blocking I/OBlocking:var fs = require('fs');var one = fs.readFileSync('one.txt','utf-8');console.log('Read file one');var two = fs.readFileSync('two.txt','utf-8');console.log('Read file two');

Non-Blocking:var fs = require('fs');fs.readFile('one.txt','utf-8',function(err,data) {

console.log('Read file one');});

fs.readFile('two.txt','utf-8',function(err,data) {console.log('Read file two');

});

DEMO

Page 21: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js: Non-Blocking I/O

Blocking I/O

Read one.txt (20ms)

Read two.txt (10ms)

Total duration (30ms)

Page 22: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js: Non-Blocking I/O

Non-Blocking I/O

Read one.txt (20ms)

Read two.txt (10ms)

Total duration (20ms)

Page 23: Event-driven IO server-side JavaScript environment based on V8 Engine

Node.js: Non-Blocking I/O

Non-Blocking I/O• Close to ideal for high concurrency / high through

put, single execution stack• Forces you to write more efficient code by

parallelizing your I/O• Feels pretty much like AJAX in the browser

Page 24: Event-driven IO server-side JavaScript environment based on V8 Engine

Node - ModulesDEMO

Page 25: Event-driven IO server-side JavaScript environment based on V8 Engine

Websockets

Page 26: Event-driven IO server-side JavaScript environment based on V8 Engine

Websockets: What is?

“The WebSocket specification—developed as part of the HTML5 initiative—introduced the WebSocket JavaScript interface, which defines a full-duplex single socket connection over which messages can be sent between client and server. The WebSocket standard simplifies much of the complexity around bi-directional web communication and connection management.

WebSocket represents the next evolutionary step in web communication compared to Comet and Ajax. However, each technology has its own unique capabilities. Learn how these technologies vary so you can make the right choice.”

Page 27: Event-driven IO server-side JavaScript environment based on V8 Engine

Websockets: What is?

• Persistent connection between browser / server• The solution of the problem with RT at browser side

var ws = new WebSocket(url);ws.onopen = function() {

// When the connection opens};

ws.onmessage = function() {// When the server sends data

};

ws.onclose = function() {// When the connection is closed

};

ws.send ('Hi all!');

ws.close();

Page 28: Event-driven IO server-side JavaScript environment based on V8 Engine

Websockets: Compatibility

Page 29: Event-driven IO server-side JavaScript environment based on V8 Engine

Websockets: Compatibility

• We can use different transports:• HTML5 WebSockets• Flash Socket• AJAX Long Polling• Forever Iframe

Page 30: Event-driven IO server-side JavaScript environment based on V8 Engine

Socket.io

Page 31: Event-driven IO server-side JavaScript environment based on V8 Engine

Socket.io

“ Socket.io aims to make realtime apps possible in every browser and mobile device,

blurring the differences between the different transport mechanism. It’s care-free

realtime 100% in JavaScript.”

Page 32: Event-driven IO server-side JavaScript environment based on V8 Engine

Socket.io: transports?

• Web Socket• Flash Socket• HTML File• XHR Polling• JSONP Polling

Page 33: Event-driven IO server-side JavaScript environment based on V8 Engine

Socket.io: How to install?

npm install socket.io

Page 34: Event-driven IO server-side JavaScript environment based on V8 Engine

Socket.ioDEMO

Page 35: Event-driven IO server-side JavaScript environment based on V8 Engine

Socket.io: Other methods?

//broadcast a message to all socketssocket.broadcast.emit('event');

//no need to internally buffer this msgsocket.volatile.emit('event');

//broadcast, nut only to people in this roomsocket.broadcast.to('room').emit('event');

//broadcast to everyoneio.sockets.emit('event');io.sockets.send('hello');

Page 36: Event-driven IO server-side JavaScript environment based on V8 Engine

Questions?

(blog): http://blog.ricardosilva.pt

(email): [email protected]