Skip to content

Instantly share code, notes, and snippets.

@cronopio
Created November 24, 2012 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cronopio/4140670 to your computer and use it in GitHub Desktop.
Save cronopio/4140670 to your computer and use it in GitHub Desktop.
BogotaJS

BogotaJS 2012

Tips for websockets on node.js

Just an example for the participants about how to use websockets on node.js. First example using ws for server and WebSocket() API of Chrome and second example using socket.io

var WebSocketServer = require('ws').Server
, MyServer = new WebSocketServer({port: 3000});
MyServer.on('connection', function(socket) {
socket.on('message', function(message) {
console.log('Recibido: %s', message);
});
setInterval(function () {
socket.send('¿Sigues mirando?');
}, 4000)
});
console.log('Demo corriendo');
var app = require('http').createServer(handler)
, io = require('socket.io').listen(app)
, fs = require('fs')
// Transportes soportados
// websocket, htmlfile, xhr-polling, jsonp-polling
io.set('transports', ['websocket'])
app.listen(3000);
function handler (req, res) {
fs.readFile(__dirname + '/index.html',
function (err, data) {
if (err) {
res.writeHead(500);
return res.end('Error loading index.html');
}
res.writeHead(200);
res.end(data);
});
}
io.sockets.on('connection', function (socket) {
socket.emit('news', { noticia: 'ninguna' });
socket.on('my other event', function (data) {
console.log(data);
});
var mensajes = 1;
setInterval(function () {
socket.emit('news', { noticia: mensajes });
mensajes++;
}, 4000);
});
<!DOCTYPE html>
<html>
<head>
<title>BogotaJS</title>
<script src="/socket.io/socket.io.js"></script>
<script>
var socket = io.connect();
var aviso = document.createElement('p');
aviso.innerHTML = 'Ninguna noticia hasta ahora...';
socket.on('news', function (data) {
console.log(data);
aviso.innerHTML = 'Recibidas ' + data.noticia + ' noticias';
socket.emit('my other event', { my: 'data' });
});
window.onload = function () { document.body.appendChild(aviso); };
</script>
</head>
<body>
<a href="http://www.bogotajs.com/">BogotaJS</a>
<p>WebSockets demo by <a href="http://github.com/cronopio">cronopio</a>.</p>
</body>
</html>
{
"name": "demowebsockets",
"version": "0.0.0-1",
"description": "un demo mas",
"main": "demo02.js",
"dependencies": {
"socket.io": "~0.9.11",
"ws": "~0.4.23"
},
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"start": "node demo02.js"
},
"repository": "",
"author": "cronopio",
"license": "BSD",
"subdomain": "bogotajs",
"engines": {
"node": "0.8.x"
}
}
@cronopio
Copy link
Author

I deployed demo02.js to nodejitsu

http://bogotajs.jit.su or http://bogotajs.nodejitsu.com

@cronopio
Copy link
Author

@cronopio
Copy link
Author

OJO Si su navegador no tiene console.log el JS del cliente reventara

@cronopio
Copy link
Author

Y por favor no usen setInterval() en la vida real

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment