Skip to content

Instantly share code, notes, and snippets.

@matiasfha
Created June 6, 2011 16:04
Show Gist options
  • Save matiasfha/1010542 to your computer and use it in GitHub Desktop.
Save matiasfha/1010542 to your computer and use it in GitHub Desktop.
nodejs + thrift +socket.io for image streaming
<script src="jquery-1.6.1.min.js"></script>
<script src="socket.io/socket.io.js"></script>
<script>
var socket = new io.Socket(null,{port:8080});
socket.connect();
socket.on('message',function(obj){
switch(obj.tipo){
case 'imagen':
img = document.getElementById('stream');
img.src="";
img.id="stream";
img.src="data:image/jpeg;base64,"+obj.data.toString();
img.width=320;
img.height=240;
break;
//To show couchdb changes
case 'notificacion':
li = document.createElement('li');
data = '<span>'+obj.data.Evento+' | ';
data+= obj.data.Fecha +' | ';
data+= obj.data.Tipo + ' | ';
data+= obj.data.VideoPath+'</span>';
li.innerHTML = data;
document.getElementById('db').appendChild(li);
break;
}
});
//To show couchdb db data at first
socket.on('connect',function(){
document.getElementById('mensajes').innerHTML = "Conectado";
$.ajax({
url: 'http://127.0.0.1:5984/test/_all_docs',
type: 'GET',
dataType: "jsonp",
success: function(response){
div = document.getElementById("db");
for(var i=0;i<response.total_rows;i++){
$.ajax({
url:'http://127.0.0.1:5984/test/'+response.rows[i].id,
type: 'GET',
dataType:"jsonp",
success:function(res){
li = document.createElement('li');
data = '<span>'+res.Evento+' | ';
data+= res.Fecha +' | ';
data+= res.Tipo + ' | ';
data+= res.VideoPath+'</span>';
li.innerHTML = data;
div.appendChild(li);
}
})
}
}
});
});
</script>
require.paths.unshift('./lib/socket.io/lib/','./lib/socket.io-node/lib/','./lib/node-couchdb/lib/');
//Couchdb changes
var couchdb = require('couchdb'),
couchdb_client = couchdb.createClient('5984','localhost'),
couchdb_db = couchdb_client.db('test');
var attach_couchdb_changes_stream = function(callback){
var stream,since;
couchdb_db.info(function(err,data){
since = data.update_seq;
stream = couchdb_db.changesStream({since:since});
stream.addListener('data',callback);
});
return stream;
}
//Server
var express = require('express'),
fs = require('fs'),
app = express.createServer();
app.get('/',function(req,response){
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();
});
});
app.get('/jquery-1.6.1.min.js',function(req,response){
fs.readFile(__dirname + '/lib/jquery-1.6.1.min.js',function(err,data){
if(err) return send404(response);
response.writeHead(200,{'Content-Type':'application/javascript'});
response.write(data,'utf8');
response.end();
});
});
app.listen(8080);
var io = require('socket.io');
var socket = io.listen(app);
socket.on('connection',function(client){
//query for changes in couchdb
attach_couchdb_changes_stream(function(change){
couchdb_db.getDoc(change.id,function(err,doc){
if(err) throw new Error(err);
var data = {id: doc._id, Evento: doc.Evento, Fecha: doc.Fecha, Tipo: doc.Tipo, VideoPath: doc.VideoPath};
client.send({tipo:'notificacion',data:data});
});
});
});
//Thrift Server
var thrift = require('thrift');
var Buffer = require('buffer').Buffer;
var ImageService = require('./ImageService.js'),
ttypes = require('./service_types.js');
//This function receive the image data (previously encode as base64 and jpeg) and send it to every client
var receiveImage = function(image,success){
socket.broadcast({tipo:'imagen', data:
image.data.toString('base64')
}
);
success(true);
}
var Tserver = thrift.createServer(ImageService,{
receiveImage: receiveImage
});
Tserver.listen(9090,function(){
console.log("Escuchando...\n");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment