Node.js (& Strapi) write to database if requests have stopped coming in since x seconds

  asynchronous, javascript, node.js, strapi

So I have multiple devices that send requests every 3 seconds to a central server. This data is saved in table A. If a device stops sending data in lets say 60 seconds, I need to log this in table B.

One way I thought of doing this is using cron jobs that loop through rows in table A and compare the updated-time field with the current time, and if it is less than 60 seconds log the event in table B. However, this seems like it could be super resource inefficient.

The other way I thought of is writing some logic in the controller. I imagine it as a function with a timeout that gets reset once another request from the same device comes in (Strapi uses async functions, so I assume it would return a response and the timeout function would not be blocking anything ). However, how could I then reset the timer?

Is there a better way to do this?

Source: Ask Javascript Questions

LEAVE A COMMENT