Detect MQTT server disconnect

If you are asking a question, please follow this template:

  1. My goal is:
    [
    Detect MQTT server not available / disconnect

Using https://github.com/mongoose-os-libs/js-demo-bundle
mongoose_os_version: 2.19.1
Running Mosquitto (MQTT) on Laptop
]
2. My actions are:
[
The code send data to MQTT is working in normal condition
#1. Disconnect the WiFi connection on MQTT server (Laptop)
#2. Stop the Mosquitto server
]
3. The result I see is:
[
Have setup a callback

  • mgos_event_add_handler(MGOS_EVENT_CLOUD_DISCONNECTED, mqtt_disconnect_cb, NULL);

#1 - Receive log print out over 20 & 50 lines of following

  • mgos_mongoose.c:66 New heap free LWM: 11536
  • E:M 18325 (2292 blocks)
    #2 - Receive callback from mqtt_disconnect_cb()
    ]
  1. My expectation & question is:
    [
    #1 No Callback event is triggered in this condition
    What other event callback can be setup ??
    ]

Did you actually check your network traffic ?
MQTT runs over TCP, if you don’t issue an MQTT disconnect nor your server does, then the disconnection detection relies on either a confirmation message for some action timeing out or the keep alive time (PING), if enabled, expiring; or the TCP connection closing.
As long as your device network connection is up, it has no way of knowing the other end is not there until something happens. I assume keepalives are enabled and so you get noticed once a PING times out.
If you shut down your Mosquitto server, it should MQTT disconnect or at least TCP close, so you’ll get a better chance of a prompt disconnect indication.
If you disconnect your device network, it should trigger a disconnect indication.