nodejs "web en tiempo real"
DESCRIPTION
La presentación sobre Node JS que hice para el Meetup de Lenguajes Dinámicos.TRANSCRIPT
![Page 1: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/1.jpg)
Web en tiempo real
![Page 2: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/2.jpg)
EVENTED I/O
![Page 3: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/3.jpg)
Server DB
MODELO TRADICIONAL
![Page 4: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/4.jpg)
Server DB
MODELO TRADICIONAL
![Page 5: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/5.jpg)
Server DB
MODELO TRADICIONAL
![Page 6: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/6.jpg)
Server DB
MODELO C/EVENTOS
![Page 7: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/7.jpg)
Server DB
MODELO C/EVENTOS
![Page 8: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/8.jpg)
MainLoop
DB
MODELO C/EVENTOS
![Page 9: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/9.jpg)
Twisted
EventMachine
![Page 10: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/10.jpg)
Evented I/O for V8 JavaScript.
![Page 11: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/11.jpg)
INSTALACIÓN
$ curl -O http://nodejs.org/dist/node-v0.2.5.tar.gz # http://nodejs.org/#download $ tar xvf node-v0.2.5.tar.gz $ cd node-v0.2.5 $ ./configure $ make $ sudo make install $ node --version
# => v0.2.5 $ node > // modo interactivo > [Ctrl+C]
![Page 13: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/13.jpg)
ESTRUCTURAFramework de bajo nivel
![Page 14: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/14.jpg)
SERVIDOR WEB
1 var http = require('http');2 3 http.createServer(function (req, res) {4 res.writeHead(200, {'Content-Type': 'text/plain'});5 res.end('Hello World\n');6 }).listen(8124, "127.0.0.1");78 console.log('Server running at http://127.0.0.1:8124/');
![Page 15: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/15.jpg)
EXPRESS 1 var app = express.createServer();2 3 app.get('/', function(req, res){4 res.send('Hello World');5 });6 7 app.listen(3000);
Node’s Sinatra
http://expressjs.com/
![Page 16: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/16.jpg)
SISTEMA DE PAQUETES
![Page 18: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/18.jpg)
WEB SOCKETS
![Page 19: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/19.jpg)
CONEXIÓN PERSISTENTE
![Page 20: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/20.jpg)
BAJO NIVEL
https://github.com/jackyyll/nodejs-websockets
1 var sys = require('sys'), 2 WebSocket = require('../websockets'); 3 4 function TimeJS () { 5 var self = this; 6 7 this.websocket = new WebSocket.Server({resource: '/time', host: 'localhost'}); 8 this.connections = new Array(); 9 10 this.websocket.addListener('connect', function (server, connection) {11 self.connections[connection.socket.fd] = "";12 });13 this.websocket.addListener('disconnect', function (server, connection) {14 clearInterval(self.connections[connection.socket.fd]);15 self.connections.splice(self.connections.indexOf(connection.socket.fd), 1);16 });17 this.websocket.addListener('receive', function (data, connection) {18 if (data == 'start') {19 self.connections[connection.socket.fd] = setInterval(function () {20 connection.write(JSON.stringify({time: new Date().toString()}));21 }, 1000);22 }23 });24 }25 26 new TimeJS();
![Page 21: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/21.jpg)
Faye is an easy-to-use publish-subscribe messaging system based on the Bayeux protocol.
It provides message servers for Node.js and Rack, and clients for use in Node and Ruby programs
http://faye.jcoglan.com
![Page 22: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/22.jpg)
INICIAR UN SERVIDOR 1 var Faye = require('faye'), 2 server = new Faye.NodeAdapter({mount: '/'}); 3 4 server.listen(8000);
![Page 23: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/23.jpg)
CREAR UN CLIENTE 5 var client = new Faye.Client('http://localhost:8000/'); 6 7 client.subscribe('/messages', function(message) { 8 alert('Got a message: ' + message.text); 9 });
![Page 24: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/24.jpg)
ENVIAR MENSAJES
10 client.publish('/messages', {11 text: 'Hello world'12 });
![Page 25: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/25.jpg)
MAS SIMPLEWeb Sockets as a Service
http://pusherapp.com/
![Page 26: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/26.jpg)
(DEMO)https://github.com/sagmor/mapsketcher
![Page 27: NodeJS "Web en tiempo real"](https://reader034.vdocuments.net/reader034/viewer/2022042715/5593b4f61a28ab306b8b45c3/html5/thumbnails/27.jpg)
Fotos: sjunnesson, atranman, denial_land, moriza, ghero79 @ Flickr