7. JavaScript: Array vs Object
Lookup
• Array
var ar = [];
for (var i = 0; i < carColors.length; i++) {
alert( ar[i] );
};
Ou
var month = ar[0];
• Object
var carColors = {};
for (var i in carColors) {
alert( carColors[i] );
};
Ou
var colorOfBWM = carColors[‘BMW’];
8. 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.”
9. 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;
}
};
10. 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);
}
12. 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.
”
13. Node.js: Companies using node!
• LinkedIn(Mobile WebApp) • Yahoo!Mail
• Ebay(Data retrieval gateway) • Rackspace(Cloud kick monitoring)
• GitHub(for Downloads) • Voxxer(Push to Talk mobile app)
• Palm/HP(in WebOS)
14. 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)
18. 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
19. 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)
20. Node.js: Non-Blocking I/O
Non-Blocking I/O
Blocking:
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
23. 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
26. 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.”
27. 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();
31. 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.”
35. Socket.io: Other methods?
//broadcast a message to all sockets
socket.broadcast.emit('event');
//no need to internally buffer this msg
socket.volatile.emit('event');
//broadcast, nut only to people in this room
socket.broadcast.to('room').emit('event');
//broadcast to everyone
io.sockets.emit('event');
io.sockets.send('hello');