← Back to the chapter · Previous detail · Next detail →

Technical detail · MQTT Python: Reliable Client Patterns

Retry settings

To keep that listener useful after a dropped link, I’ll make its recovery steps explicit. Register on_message and connect to the broker. Subscribe to the topics you need, for example in on_connect, so they are restored on reconnect. Keep the network loop running so message callbacks can run. Use exponential backoff: increase the wait between retries up to a cap. In Paho, reconnect_delay_set() sets the wait range; it does not add jitter or a retry-count limit. Jitter is a random change in each wait that spreads retries across clients. This can keep an outage from leaving the client silent or overloading the broker. Use loop_start() if the app must read sensors while networking runs in a background thread.

← Back to the chapter · Previous detail · Next detail →