mysql - Socket.io for checking updates from database -


i have node.js server connects mysql database , opens new socket using socket.io. role of server notify client (user) connects when there's new message user in database table. code below works if client explicitly emits 'check_messages' request. how can change client 1 that's notified whenever new message inserted in mysql table user instead of client having explicitly emit 'check_messages' request?

var app = require('http').createserver().listen(8124);  var mysql      = require('mysql'); var connection = mysql.createconnection({   host     : 'localhost',   user     : 'some username',   password : 'some password',   database: 'some database' });  connection.connect();  console.log('server running @ http://127.0.0.1:8124/');  var io = require('socket.io').listen(app);  var prev_id = 0;  io.sockets.on('connection', function (socket) {   socket.emit('greeting', 'hello');   socket.on('check_messages',function(data){   var uid = data['uid'];   var q = "select * messages user_id="+uid+" order id desc limit 1";   connection.query(q, function(err, rows, fields) {       if (err) throw err;       if (rows[0].id > prev_id){         socket.emit('new_message',rows[0]);         prev_id = rows[0].id       }     });   }); }); 

if don't want polling on database can use postgresql database support listen/notify. notified when there modification on table.

implementation example


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -