Server.setsendtimeoutpolicy() to trigger API

is it possible to use server.setsendtimeoutpolicy() to trigger an api on my website when the device disconnects? So the idea is to trigger an API that sends a text message to the user informing them their device lost connection and will not work as expected.

Also, is it possible to trigger the api when the device is back on wifi to inform the user that it is now online?

Thank you for your time and any responses.

Have you considered device.onconnect(function) and device.ondisconnect(function)?

The disconnection side is not so responsive. You may want to consider your own heartbeat message from device to agent. You agent could then alert your website to the failure to receive a heartbeat.

Honestly, I had no idea about device.ondisconnect(function). I believe that should work perfectly. Because I could do a json request to my api when the agent runs that function. no? And what do you mean by “The disconnection side is not so responsive” ? Thank you!

It can take a couple of minutes for a disconnection to be recognized by the server. This is because TCP assumes a connection is alive when idle; there are regular probes sent out by the server to verify this (the client responds to these when it is connected) and only after a number of missed probes does the server decide the device has gone away.

I see, as long as it reacts within less then five minutes it should be fine. From what I tested it usually realizes in about 2 minutes which is good enough for me.