Skip to content

MQTT Client Bridges

Setting up the MQTT bridge

Table of Contents

What is the MQTT bridge? An MQTT bridge lets you connect two MQTT brokers together. They are generally used for sharing messages between systems. A common usage is connecting edge MQTT brokers to a central or remote MQTT network(google). The Mosquitto broker (server) can be configured to work as an MQTT bridge.

In this project, I will show you how to create an MQTT Bridge for a client broker(client-mqtt-broker) which will be bridging to the central or remote MQTT Broker(Mqtt-Protected)

How to configure an MQTT bridge for a client broker and factory broker

Prerequisite

On this section, I will show you the configurations for creating a MQTT bridge for a specific distributor as a client broker. You should have an existing distributor account on the ERM to be able to create an MQTT bridge for a client broker.

Topic Bridging

The general format is;

topic pattern direction QOS local prefix/remote prefix.

Direction can be out, in, or both;

  1. out(uplink) = publish from client broker to the central broker(low to high)
  2. in(downlink) = receive from central broker to client broker(high to low)
  3. both = publish and receive

NB: MQTT has two types of wildcards:

  1. Multi-level wildcard: The multi-level wildcard symbol is “#”. It is used to match multiple levels in a topic hierexplarchy, including zero or more levels. It’s important to note that using multi-level wildcards can result in a large number of messages being received, which can have an impact on the performance of the MQTT broker and the subscriber. Therefore, it’s recommended to use wildcards with caution and only when necessary.
  2. “+” means literally everything but only one level, so one or more may be used inside a topic.

QOS

Quality of Service (QoS) refers to the level of guarantee for message delivery between a publisher and subscriber. MQTT supports three levels of QoS:

  1. QoS 0: At most once delivery - The message is sent only once without any guarantee of delivery. The sender does not receive any acknowledgment from the receiver.
  2. QoS 1: At least once delivery - The message is sent at least once and the sender receives an acknowledgment from the receiver. If the acknowledgment is not received, the message is resent.
  3. QoS 2: Exactly once delivery - The message is sent exactly once and both the sender and receiver maintain a state to ensure that the message is delivered only once. This level of QoS provides the highest level of reliability but also incurs the highest overhead.

Below is the snippet for creating an MQTT bridge for the client bridge which should be added in the mosquitto.conf file for a broker.

*# =================================================================# All Configs Start# =================================================================*

listener 18884
 *#This client listens to port number 18884 that is hard coded, in future the port will be automatically assigned*

connection bridge-name
 *#Set the name of the connection between the local and remote broker*
address mqtt-2.omnivoltaic.com:8883
*#This is the central broker address which is hardcoded*

topic dt/V01/GPRSV2/willingpengfleet/# **in** 0
topic dt/# out 0
*#Every topic that starts with dt from client broker is allowed to uplink to central broker*

allow_anonymous false
password_file /mosquitto/config/password.txt

*# Files used to access the central broker using tls*
bridge_cafile /mosquitto/config/ca.crt
bridge_certfile /mosquitto/config/client.crt
bridge_keyfile /mosquitto/config/client.ke

remote_username username
remote_password password
*# Remote username and password are the login credentials to the central broker*
tls_version tlsv1.2

*# =================================================================# All Configs End# =================================================================*

How to configure a MQTT bridge for a factory broker.

*# =================================================================# All Configs Start# ================================================================*

listener 18883
connection factory-broker-bridge
address mqtt-2.omnivoltaic.com:8883
*# This is the central broker address which is hardcoded.The broker listens to port number 18883*
topic cmd/# **in** 0
*# Factory broker will subscribe to all downlink topics begining with cmd*
topic dt/# out 0
*# every topic beginning with dt from factory broker is allowed to uplink to central broker*

allow_anonymous false
password_file /mosquitto/config/password.txt

bridge_cafile /mosquitto/config/ca.crt
bridge_certfile /mosquitto/config/client.crt
bridge_keyfile /mosquitto/config/client.key
bridge_insecure false
*#Files used to communicate with the central broker*
remote_username Admin
remote_password 7xzUV@MT

tls_version tlsv1.2
require_certificate true

*# =================================================================# All Configs End# =================================================================*

### Setting up a MQTT bridge on Termux

Prerequisites:

  • Have Termux installed on your Android Device.
  • Open Termux on your Android Device.
  • Create a folder Mosquitto using Termux and check the Directory to the folder

mkdir Mosquitto

cd Mosquitto

  • Inside the folder Mosquitto, create a file named mosquitto.conf.
  • Copy the following content inside the file mosquitto.conf
```sh
      # =================================================================
      # All Configs Start
      # =================================================================

      listener 1884

      connection testing-bridge2
      address mqtt-2.omnivoltaic.com:1885
      topic # both 0

      allow_anonymous true

      # =================================================================
      # All Configs End
      # =================================================================
      ```
  • The below is the snippet for creating a MQTT bridge for Edge/Hub(Termux) Layer bridge which should be added in the mosquitto.conf file for a broker. The broker will bridge to one of the client brokers.

Run the following command to start

mosquitto -c mosquitto.conf -d

### Setting up a MQTT bridge on tickstack

  • On this section, I will show you the configurations for creating a MQTT bridge for a specific distributor as a client broker. NB: You should have an existing distributor account on the ERM to be able to create a MQTT bridge for a client broker.

Prerequisites:

  1. Have docker desktop installed.
  2. Have git installed.
  3. Have a Terminal.
  4. Have any code editor, I’d recommend VSCODE. - Have the docker-tickstack application on your PC. If you don’t have use the documentation to install it.
  5. Open the tickstack project using a terminal of your choice.
  6. Change Directory to mosquitto/config like below

    cd mosquitto/config

  7. Inside the folder config, create a file named mosquitto.conf if it doesn’t exist.

  8. Copy the following content inside the file mosquitto.conf
```sh
    # =================================================================
    # All Configs Start
    # =================================================================

    listener 1884

    connection testing-bridge2
    address mqtt-2.omnivoltaic.com:1885
    topic # both 0

    allow_anonymous true

    # =================================================================
    # All Configs End
    # =================================================================
    ```
  1. Run the following command to start the project sh docker-compose up -d