Currently I’m using a timer with node-schedule to check for changes in the database, making queries every 1 minute and then comparing with the information already registered to see if there were changes since the last check.
With what I want to do I want to decrease the amount of unnecessary queries and be able to bring the data in real time, and this is interesting because I am using this timer to provide statistics on the amount of users, categories and articles.
I am using Knex as query-builder in my db.
This is my file with the timer (I am using MongoDB to store this information):
const schedule = require('node-schedule')
module.exports = app => {
schedule.scheduleJob('*/1 * * * *', async function () {
const usersCount = await app.db('users').count('id').first()
const categoriesCount = await app.db('categories').count('id').first()
const articlesCount = await app.db('articles').count('id').first()
const { Stat } = app.api.stat
const lastStat = await Stat.findOne({}, {},
{ sort: { 'createdAt': -1 } })
const stat = new Stat({
users: usersCount.count,
categories: categoriesCount.count,
articles: articlesCount.count,
createdAt: new Date()
})
const hasChanged = !lastStat
|| stat.users !== lastStat.users
|| stat.categories !== lastStat.categories
|| stat.articles !== lastStat.articles;
if (hasChanged) {
stat.save().then(() => console.log('[STATS] Estatísticas atualizadas!'))
}
})
}
2
Answers
You can store the statistics in mongodb, and use
watch
to monitor changes.https://www.mongodb.com/docs/drivers/node/current/usage-examples/changeStream/
PS: suggesting this because you mentioned you are using mongodb to store the statistics.
Postgres supports LISTEN and NOTIFY commands that can be used to inform other applications of changes in the db. These can be used to avoid polling.
socket.io’s postgres-adapter supports notifications over websockets.
This video may also be helpful.