Socket.io for checking updates from database
You can run your code in timer's event handler on server.
The code below checks database for new message for every 5 seconds and emits event if necessary
io.sockets.on('connection', function (socket) { socket.emit('greeting', 'Hello'); setInterval(5000,function(data){ var uid = data['uid']; var q = "SELECT * FROM messages WHERE user_id="+uid+" ORDER BY 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 } }); });});
As alternative way I think you could implement message queueing using redis with the fast node_redis client. It has built-in pubsub semantics.
Look at Redis. It is fast NoSQL key-value storage which you can use to organize fast message queueing. Use node_redis npm module to communicate with it. Read this reference