I'm working on the application where I want to get notified when the publisher of the topic goes offline. I'm using setInterval() for sending the ping-pong every 2sec and using timeOut(8sec) for checking if pong received,but it not working as timeOut can break in js if the call stack is not empty, so can anyone help me?
Asked
Active
Viewed 127 times
0
-
You need to look at MQTT's Last Will and Testament feature. – hardillb Sep 11 '21 at 10:37
-
@hardillb I'm using it but it is sending the lastWill after 2mins of wifi disconnect, so not getting how send instantly – Meghraj Deshmukh Sep 11 '21 at 10:49
-
You can change keep alive timeout. – hardillb Sep 11 '21 at 12:41
-
The answer here really is the LWT with a suitable keepalive value. But no solution will give you instant notification. If you want help with your ping solution [edit](https://stackoverflow.com/posts/69142010/edit) the question to show the code and add a better description of what's not working. – hardillb Sep 11 '21 at 18:53
-
You only get an immediate last-will if the subscriber is able to cleanly disconnect from the broker (sending `DISCONNECT` packet). If the subscriber is ruggedly disconnected by losing wifi connection e.g., the broker has to wait `keepalive * 1.5` until disconnecting the client and publishing its lwt – Odysseus Sep 13 '21 at 07:11
-
1@Odysseus are you sure? The [spec](http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/os/mqtt-v3.1.1-os.html#_Toc384800467) states "On receipt of DISCONNECT the Server MUST discard any Will Message associated with the current connection without publishing it" and the time taken to detect a loss of connection depends on a range of factors ([this article](https://www.hivemq.com/blog/mqtt-essentials-part-10-alive-client-take-over/) provides a good overview). – Brits Sep 13 '21 at 09:58
-
@Brits You are right, my fault - LWT is only published on unexpected disconnects and not if the client has properly closed the connection – Odysseus Sep 13 '21 at 10:20