I am attempting to poll a MySQL DB by iterating every 2 seconds and retrieving all records created less than 2 seconds ago.
If I use setInterval it seems to occasionally miss a record. If I use setTimeout it only fetches the first iteration.
How can I fetch the data more reliably to include all data?
io.on('connection', function(socket) {
authorize (socket, function(shop_id) {
setInterval(function() {
var sql = "SELECT * FROM `stats` WHERE `shop_id` = " + connection.escape(shop_id) +
" AND `created` > DATE_SUB(NOW(), INTERVAL 2 SECOND)";
var query = connection.query(sql, function(err, rows, fields) {
if (err == null) {
console.log (rows);
socket.emit('newData', rows);
}
});
}, 2000);
});
});