eclipse-mosquitto / mosquitto

Eclipse Mosquitto - An open source MQTT broker
https://mosquitto.org
Other
9.1k stars 2.4k forks source link

Create a bridge, the client switches the connection node, and receives duplicate messages #2334

Open fly2086 opened 3 years ago

fly2086 commented 3 years ago

mosquitto.conf

=================================================================

Bridges

Current node:192.168.1.6:1883

=================================================================

connection nodeA address 192.168.1.5:1883 topic # both 0

Steps

1.client sub mosquitto_sub -h 192.168.1.5 -p 1883 -v -t testtopic -i id -q 2 -c

2.server pub mosquitto_pub -h 192.168.1.6 -p 1882 -t testtopic -m helloworld -q 2

3.client connects to another node mosquitto_sub -h 192.168.1.6 -p 1883 -v -t testtopic -i id -q 2 -c

question: Why can the client receive the same message on both nodes?

Elix-g commented 3 years ago

topic # both 0

means, that the broker which is configured to establish a bridged connection, both, send and receive all topics to/from the 2nd broker. A bridge does not cause brokers to work like a single instance. A broker being a bridge, acts like a normal broker but with client function against another broker. So, published messages, depending on the bridge configuration, may be duplicated.

fly2086 commented 3 years ago

topic # both 0

means, that the broker which is configured to establish a bridged connection, both, send and receive all topics to/from the 2nd broker. A bridge does not cause brokers to work like a single instance. A broker being a bridge, acts like a normal broker but with client function against another broker. So, published messages, depending on the bridge configuration, may be duplicated.

If I want the client to switch any node at any time without receiving repeated messages, how should I configure it?

Elix-g commented 3 years ago

You can just let the broker that holds the bridge configuration add some prefix topic upon send/receive messages as per mosquitto.conf man page:

https://mosquitto.org/man/mosquitto-conf-5.html

Chapter "Configuring bridges", description of the keyword "topic".

As a result, sent/received messages have another topic on the second broker than on the first one. Of course, your message can be received by the client just on one broker, not on any. Nonetheless, I wonder about your desired target.