So I have setup two MQTT brokers [say 'A' and 'B'](setup using cloud mqtt) where 'B' is bridged to 'A' (by a two way bridge). When I publish to a topic to 'A' it shows up under 'A' then shows up under 'B' (as expected). But, if i publish to 'B' it shows up under 'B' then under 'A' then a duplicate publish is seen under 'B'. Is there some flag I need to set or setting i need to change to prevent this ?
How did you initiate the bridge?
You need to define a local prefix and a different remote prefix for the connection. Do not mix that up with the meaning of "local" or "remote", but thake them as the names of those branches that should be mirrored to the other broker. This prevents the brokers from looping the very same messages again and again.
Lets say your "A" broker sends commands over the branch order/command and your "B" broker holds sensor values at home/sensor
Then if you publish a message to order/command/ on broker "A", you will see this message unter the same branch order/command/ at broker "B".
Also every sensor value thay may be published to home/sensor/ at broker "B" will be mirrored to home/sensor/ at broker "A"
mosquitto.conf: (Raspberry "A" is 192.168.1.100)
connection raspi-PC
address 192.168.1.110:1883
topic # both 0 order/command/ home/sensor/
Then this will work:
client1 : mosquitto_sub -h 192.168.1.110 -t order/command/#
client2 : mosquitto_sub -h 192.168.1.100 -t home/sensor/#
clientA : mosquitto_pub -t order/command/message -m "Hello from A"
clientB : mosquitto_pub -t home/sensor/temp -m "22.5C"
client1 --> order/command/message/"Hello from A"
client2 --> home/sensor/temp/"22.5C"
You see that client1 and client2 are cross subscribers to broker "A" and "B" and they get all branch and subbranch messages, but they are not necessarily local to the brokers.
On clientA and clientB I used the local client to send the message to the local broker.