Event-driven IO server-side JavaScript environment based on V8 Engine
-
Upload
ricardo-silva -
Category
Technology
-
view
4.180 -
download
1
description
Transcript of 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]
Event-driven I/O server-side JavaScript environment based
on V8 Engine
JavaScript(examples)
JavaScript: Array vs ObjectInstantiation
• Arrayvar ar = []; /* ou new Array(); */
• Objectvar carColors = {}; /* ou new Object();*/
JavaScript: Array vs ObjectAdd
• Arrayvar ar = []; ar[0] = 'January';Ou ar.push(‘March’);
• Objectvar carColors = {};carColors['BMW'] = 'Red';carColors['Audi'] = 'Blue';
JavaScript: Array vs ObjectRemove
• Arrayar.splice(0,1);
• Objectdelete carColors['BMW'];
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’];
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.”
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; } };
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); }
Node.js
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.
“
”
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)
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)
Node.jsDEMO
Node.js
“ Come on…, server side JS has been
around since 1996 …”
Node.js
“ …what is so special about node?
”
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
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)
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
Node.js: Non-Blocking I/O
Blocking I/O
Read one.txt (20ms)
Read two.txt (10ms)
Total duration (30ms)
Node.js: Non-Blocking I/O
Non-Blocking I/O
Read one.txt (20ms)
Read two.txt (10ms)
Total duration (20ms)
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
Node - ModulesDEMO
Websockets
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.”
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();
Websockets: Compatibility
Websockets: Compatibility
• We can use different transports:• HTML5 WebSockets• Flash Socket• AJAX Long Polling• Forever Iframe
Socket.io
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.”
Socket.io: transports?
• Web Socket• Flash Socket• HTML File• XHR Polling• JSONP Polling
Socket.io: How to install?
npm install socket.io
Socket.ioDEMO
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');