semunimed2022
based on http://www.steves-internet-guide.com/mosquitto-bridge-configuration/
The code of this section is here
An MQTT broker can be executed as a Docker container. This fact provides flexibility when deploying these services, especially in edge devices, like Raspberry Pi or similar.
Searching for MQTT, you'll get:
The first one, eclipse-mosquitto, is the official image for the Mosquitto MQTT broker, probably the most widely used MQTT broker.
The third one, efrecon/mqtt-client is also a helpful image that allows having a handy client to test deployments.
The docker-compose.yml
file below indicates how to start up an MQTT broker locally:
Executing docker compose up
you will get something like:
giving:
this is obtained with a ./mosquitto/config/mosquitto.conf
file as simple as this:
We can do a quick test by opening two terminals and executing:
in one of them (check the use of --network=host
), and:
in the other. We basically created a subscriber and a publisher containers that are talking through our broker.
An MQTT bridge lets you connect two MQTT brokers together. They are generally used for sharing messages between systems.
A common usage is to connect and edge MQTT brokers to a central or remote MQTT network. A Mosquitto broker (server) can be configured to work as an MQTT bridge.
Generally the local edge broker will only bridge a subset of the local MQTT traffic.
You only need to configure one of the brokers to act as the bridge, the other will act as a normal broker.
When you configure the Mosquitto broker to act as a bridge it becomes an MQTT client to the remote broker and subscribes and publishes to topics on the other broker just like a normal MQTT client.
You will need to configure
Note: You can configure multiple remote broker addresses and the broker will switch to another broker if the current broker connection fails.
What topics are being bridged, and if they are being remapped is controlled by the topic entry. Wildcards can be used in the topic entry.
The general format is
direction
can be:
The mosquitto.conf
below shows how to bridge all topics with no remapping.
try_private [ true | false ]
If try_private is set to true, the bridge will attempt to indicate to the remote broker that it is a bridge not an ordinary client. If successful, this means that loop detection will be more effective and that retained messages will be propagated correctly. Not all brokers support this feature so it may be necessary to set try_private to false if your bridge does not connect properly.
Note: If you aren’t using topic prefixes then you don’t need to add them.
Supponsing the configuration below, will see some example
Example 1:
Configuration file topics
Result:
house/sensor1
or house/anything
and the message is sent to broker 2room/sensor1
and the message is not sent to broker 2Example 2:
Configuration file topics
Result:
house/sensor1
or house/anything
and the message is sent to broker B1room/sensor1
and the message is not sent to broker B1Example 3:
or
The entries:
topic house/sensor1 in 0 “” “”
topic house/sensor1 out 0 “” “”
Are equivalent to the single entry:
topic house/sensor1 both 0 “” “”
and also
topic house/sensor1 both 0
Result:
Remapping is obtained using the local and remote prefixes.
For example, when the bridge on broker1 subscribes to a topic on broker2 it uses the form:
remote_prefix + topic name
e.g. b2/house/sensor1
When the bridge on broker1 subscribes to a topic on broker1 it uses the form:
local_prefix + topic name
e.g. b1/house/sensor1
The mosquitto.conf
below shows a remapping configuration for broker 1:
With the above configuration then:
house/sensor
is published to broker 2 on topic b1/house/sensor
.Broker2 is functioning as a normal broker (not a bridge) and doesn’t need any extra configuration.
In this case we will configure the local brocker running as a container to bridge with a cloud broker, more specifically, flespi.
We have to modify the mosquitto.conf
file as indicated below. This is the most basic configuration where everything is forwarded between the two brokers.
Executing docker compose up
now, we obtain a sligthly differente result:
We can do a quick test by opening two terminals and executing:
in one of them, and:
in the other.
The simmetrical scenario is as follows:
and:
We have to modify the mosquitto.conf
file as indicated below. In this case we are testing a scenario where a remapping of the topics is preformed:
We can test the first rule by opening two terminals and executing:
in one of them, and:
in the other. Now, nothing will appear… we have to modify the local subscriber as follows:
to get the data.
The simmetrical scenario is as follows:
and:
It is possible to bridge to another broker in the event of a broker failure.
To do so you need to set multiple address:por
entries in the address field i.e
will use broker 192.168.1.185 if the connection to the first broker (192.168.1.168) fails.
Here is the description taken from the online manual:
round_robin [ true | false ]
If the bridge has more than one address given in the address/addresses configuration, the round_robin option defines the behaviour of the bridge on a failure of the bridge connection. If round_robin is false, the default value, then the first address is treated as the main bridge connection. If the connection fails, the other secondary addresses will be attempted in turn. Whilst connected to a secondary bridge, the bridge will periodically attempt to reconnect to the main bridge until successful.
If round_robin is true, then all addresses are treated as equals. If a connection fails, the next address will be tried and if successful will remain connected until it fails.
Looping
Publishing and subscribing to the same topic on the broker could result in a loop.
However if you try this you find that a broker detects that another broker sent the message, and doesn’t send the message back to it even though the broker has subscribed to that topic.
This behaviour is not 100% clear, since no reference for broker detection can be found in the specs.
Exercise:
Modify the docker-compose.yml
file to start 2 brokers locally that perform full bridging among them