I am working on a IOT device and the backend on NodeJs, I am pinging my server every 15 secs to determine the device status. But I want to know a mechanism to update database when there is no ping from device after 15 secs. Thanks in Advance
Set a timeout every time a ping is received. Cancel previous timeouts. Note... you might want to add a fudge factor to your timeout to reduce noise)
var timerId;
function ping(deviceId) {
clearTimeout(timerId);
timerId = setTimeout(function() {
updateDb(deviceId);
}, 15000 + 500);
}
I'm doing something similar to you but will have 10,000 devices pinging every 15 minutes. I'll be using a different solution. I have a cron job (technically a Cloudwatch Event) that runs every 5 minutes to check which devices haven't pinged within the last 15 minutes.
that seams to be a job for a socket. 15 seconds no event means setTimeout gets triggered :)
or do you need a more complex mechanism ? for me it's the classic "delay event" principle.
Juha Lindstedt
Creator of RE:DOM and Liike, web architect
I would do it so that you just refresh some kind of "lastseen" value in the db, which you can later use to determine, how long it's been offline..