Skip to content

Instantly share code, notes, and snippets.

@matiasfha
Created June 2, 2011 20:19
Show Gist options
  • Save matiasfha/1005218 to your computer and use it in GitHub Desktop.
Save matiasfha/1005218 to your computer and use it in GitHub Desktop.
nodejs + thrift +socket.io + couchdb
<!DOCTYPE>
<html>
<head>
<title>Test</title>
</head>
<body>
<div id="mensajes">
Hola Mundo
</div>
<div id="db">
No change on DB
</div>
&nbsp;
<script src="socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket(null,{port:8080});
socket.connect();
socket.on('message',function(obj){
document.getElementById("mensajes").innerHTML = obj;
switch(obj.tipo){
case 'imagen':
//DO something
break;
case 'notificacion':
document.getElementById('db').innerHTML="DB Change!!";
break;
}
});
socket.on('connect',function(){
document.getElementById('mensajes').innerHTML = "Connected"
});
</script>
</body>
</html>
//HTTP Server
var http = require('http'),
url = require('url'),
fs = require('fs'),
sys = require('sys');
var hserver = http.createServer(function(request,response){
var path = url.parse(request.url).pathname;
switch(path){
case '/':
fs.readFile(__dirname + '/index.html',function(err,data){
if(err) return send404(response);
response.writeHead(200,{'Content-Type':'text/html'});
response.write(data,'utf8');
response.end();
});
break;
default: send404(response);
}
}),
send404 = function(res){
res.writeHead(404);
res.write('404');
res.end();
};
hserver.listen(8080);
require.paths.unshift('./lib/socket.io/lib/','./lib/socket.io-node/lib/','./lib/node-couchdb/lib/');
var io = require('socket.io');
var socket = io.listen(hserver);
//Thrift Server listen for a C++ client .. read the data and send it to webpage using //socket.io
var thrift = require('thrift');
var Buffer = require('buffer').Buffer;
var ImageService = require('./ImageService.js'),
ttypes = require('./service_types.js');
//when data is received from c++ app, push the data to webpages
var receiveImage = function(image,success){
socket.broadcast({tipo:'imagen', data:
image.data.toString('base64')
}
);
success(true);
}
var Tserver = thrift.createServer(ImageService,{
receiveImage: receiveImage
});
//listen in 9090 for thrift clients
Tserver.listen(9090,function(){
console.log("Escuchando...\n");
});
//DB connection (couchdb)
var couchdb = require('couchdb')
var couchdb_client = couchdb.createClient('5984', 'localhost');
var couchdb_db = couchdb_client.db('test');
couchdb_changes = function(callback) {
var stream;
var since;
couchdb_db.info(function(err, data) {
if (err) throw new Error(JSON.stringify(err));
since = data.update_seq;
stream = couchdb_db.changesStream({since:since});
stream.addListener('data', callback);
});
return stream;
}
socket.on('connection', function(client){
couchdb_changes(function(change) {
couchdb_db.getDoc(change.id, function(er, doc) {
if (er) throw new Error(JSON.stringify(er));
/*var data ={tipo:'evento',data:{id: doc._id, Evento: doc.Evento, Fecha: doc.Fecha, Tipo: doc.Tipo, VideoPath: doc.VideoPath}};
client.send(JSON.stringify(data)); //send json data*/
var notificacion = {tipo:'notificacion'};
client.send(JSON.stringify(notificacion));
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment