'How to connect to multiple instances of MQTT broker with Paho Python client?

I want to use MQTT protocol, and I have 2 instances of my broker (i.e. ActiveMQ) running on ip1 and ip2. At a particular time only 1 instance will be up and the other instance will be offline. Is there a way to connect to multiple instances using Paho's Python client?

For example when using the STOMP client we can have an array of (ip, port) combinations like this:

conn = stomp.Connection(host_and_ports=[("ip1", port1), ("ip2", port2)])

Is there a similar approach possible for MQTT (maybe using a different package)?



Solution 1:[1]

No, the Python Paho client does not support supplying a list of brokers to try and connect to in a round robin or fallback situation.

The closest approach would be to use a DNS entry that points to multiple IP addresses and allow the DNS to do the round robin resolution, but would need to double check that it doesn't cache the result of the first lookup and re-use that and it wouldn't allow for different port numbers.

The client does support doing SRV DNS look ups which could include a port number, but again you would need to test this to see if it caches results.

I'm sure the Paho Python project would accept a Pull Request to add this functionality.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 hardillb