Reputation: 331
I need to use setInterval
to do queries to database each N seconds and fire results to all Socket.IO
clients so I do it like this:
let interval_id = null
io.on('connection', function(socket) {
if (interval_id == null) {
interval_id = setInterval(function() {
db.table.items.getAll().then(function(items) {
process.emit('items_found', items)
}).catch(function(err) {
log.error(err)
})
}, config.scan.interval)
}
process.on('alarms_found', function(alarms) {
console.log(alarms.length)
})
})
It's working fine but I'm newbie in NodeJS
and I don’t know any other ways to do this... In general I understand that usage of global scope is not the best idea, but I don’t know others...
Upvotes: 3
Views: 402
Reputation: 1374
Upvotes: 2