gprade
gprade

Reputation: 31

Recovery by MQTT client for MQTT server failure/restart

I have a long-running paho-MQTT (Python 3) client. The client is listen-only - it subscribes to topics and acts on those inputs but it does not publish. Everything runs fine until the server becomes unresponsive (server restart or network transport failure); at that point it becomes unresponsive since the connection is broken. The subscribes are all QOS=0.

What mechanism exists to alert the client that the server is inop? Do I need to manually check for stale input or is there a call-back or exception that will get thrown? If stale input is detected, what's the best practice for recovery to re-establish the subscriptions?

Upvotes: 2

Views: 1518

Answers (1)

hardillb
hardillb

Reputation: 59771

As described in the Paho Python docs

on_disconnect()

on_disconnect(client, userdata, rc)

Called when the client disconnects from the broker.

  • client, the client instance for this callback
  • userdata, the private user data as set in Client() or user_data_set()
  • rc, the disconnection result

The rc parameter indicates the disconnection state. If MQTT_ERR_SUCCESS (0), the callback was called in response to a disconnect() call. If any other value the disconnection was unexpected, such as might be caused by a network error.

On Disconnect Example

def on_disconnect(client, userdata, rc):
    if rc != 0:
        print("Unexpected disconnection.")

mqttc.on_disconnect = on_disconnect

Upvotes: 0

Related Questions