Mosquitto Mqtt Bridge

 The Eclipse moquitto provides the open source message Broker which supports the Mqtt Protocol versions 5.0,3.11,3,1.The Mqtt protocol is a very lightweight and can be operated on microcontroller based systems like Raspberry Pi to computer PC servers. The Mqtt clients can be operated from the small microcontroller based sensor which are constrained.

The Mosquitto Brokers can be installed on your systems by visiting the Eclipse mosquitto Download page.Here the setup file suitable to various os can be downloaded and installed. If you want to know how to setup the Mosquiito Broker on WIndows PC then click here.


Mqtt Bridge

Mqtt bridge is used to connect two brokers together.The use of the Mqtt bridge is to connect edge Brokers with r=the remote or central broker.The data of the local clients can be shared with the  Remote or central server over the shared topic.The mqtt Bridge acts as a mqtt client for the Remote broker with which the data is shared with the shared topic. In fact , in mqtt Bridge,one broker acts a bridge and other acts as a normal broker.


Mosquitto Mqtt Bridge


The above figure shows the setup for Mosquitto Mqtt Bridge. There are two brokers, the Broker1 and and Broker2. The  Broker1 is acting as a Brigdge Broker and is available on local host and listens to the port 1884.The Broker2 acts as a normal Broker can be a central or Remote Broker. The client A is connected to the Broker1 and the client B is connected to the Broker2.


Configure the Broker as a Bridge

To configure the broker as a bridge,the followings things are to be set in the .config file.

1. The Address and Port of the remote Broker or Brokers using the keyword Address.

2. The client name using the connection name keyword.

3. The topics to be published by the Broker and topic to which it is subscriber under the topic keyword.


Bridging  topics

The topics which are to be shared are represented in the format as given below.

topic patterns direction QoS local Prefix/Remote Prefix

where

pattern - it can be represented by # as wild card topic or topic name.

Direction - it can be in,out or both. The in means the topic is subscribed to the Broker.The out is used to publish the topic data.Both is used when the topic is to published and subscribed to

QoS - It represents the QoS of service.It can take the values 0,1,2.

Local Prefix/Remote Prefix-The topic remapping can be done using the prefixes like local or remote.

 Example 1

Consider the file topic

topic  door/#  out

When topic is configured like this then

1. When clientA  publishes the door/stat or door/ anything then the data reaches to Broker2.

2.When Client A publishes the data on the topic door1/stat or door1/amything then data does not reach Broker2.

3.When Cient B publishes the data on any topic then it does not reach Broker1.


Example 2

Consider the file topic

topic door/# in

When topic is configured like this then

1. When Client A publishes the data on topic door/stat or door/anything then Broker 2 receives nothing.

2.When Client B publishes the data on the door/stat or door/anything then the data reaches Broker1.


Example 3

consider the file topic

topic door/stat both 0

When the topic is configured like this then

1. When ClientA publishes the data on topic door/stat then data reaches broker2.

2.When ClientB publishes the data on topic door/stat then the data reaches broker1.


Realtime Example

Create the test_br.conf file for the Broker bridge with the settings as shown in the figure below.


BridgeConfig file


As per the test_br.conf file,the bridge broker is listening on port 1884.The address of the broker2(normal broker)  is IP 192.168.1.100  and port is 1883.The topic set is

topic # both 0

The topic can be a wild card, operating in both the directions i.e. in and out. This means the data over any topic # can be published from the broker1 Bridge to broker2,The Bridge broker can receive any topic data over any topic.

Create the test.conf file for Broker2 which is a normal broker. The broker is a normal and it listens to port 1883.


Test Bridge broker setup




First the broker2 which is a normal broker is started with test.conf file.The broker2 which is Bridge Broker is started. The image shows the Broker2 is connected with Broker1 as client.

The Subscriber client subscribes with the Broker1 over topic house/test

The Publisher client publishes the message "hello" over topic  house/test to the broker2.The Broker2 transmits message to the broker1 and the message arrives at the subscriber's end as shown in the image below.




When Publisher client publishes the message  over topic  house/test to the broker1.Then the message transmitted from the Broker2.The broker2 transmits message to subscriber..


Mqtt Bridge and remapping of Topics

The  topic remapping is done using the prefixes local prefix and remote prefix.

Example

Consider the topic case as

topic  house/sensor out 0  b1/ ""

topic house/lamp in 0 "" b2/ 

here local prefix is b1 and remote prefix is b2.

If this is the topic configuration then

When Broker1 receives the message from clientA connected to it over the topic b1/house/sensor then

this message reaches broker2 with topic as house/sensor and reaches the clientB

When Broker2 receives the message from ClientB connected to it over the topic b2/house/lamp

then this topic message reaches broker1 with topic house/lamp and delivered to client A.


Realtime Example

Edit the test_br,conf file as shown in the image below.




Here the topic specified as 

topic house/sensor out 0 b1/ ""

topic house/lamp in 0 "" b2/

The client A can publish the message to the Bridge Broker over the topic b1/house/sensor. The 

The client B can publish the message to the normal Broker over topic b2/house/lamp



In the image as shown above, the publisher client sends the message "hello" to the bridge Broker over the topic b1/house/sensor. This message is sent to the normal broker which is in turn sends the message to the subscriber which is connected to the normal broker.



In the image as shown above, the publisher client sends the message "hello" to the normal Broker over the topic b1/house/lamp. This message is sent to the Bridge broker which is in turn sends the message to the subscriber which is connected to the normal broker.

For more details Watch the you tube video here.









Post a Comment

Previous Post Next Post