Tutorial: Forward to and from Mosquitto

Configure FairCom's MQTT broker to forward and subscribe to messages from a Mosquitto broker

 

You can chain together MQTT brokers by forwarding messages from one broker to another. This is a great way to move information across data centers (see Broker forwarding).

The MQTT broker in FairCom MQ and FairCom Edge can act as a bridge to another MQTT broker. You can use the API Explorer tool to configure FairCom's MQTT broker engine.

This tutorial contains instructions for configuring FairCom's MQTT broker to forward messages to a Mosquitto broker. It also shows how to subscribe to messages from Mosquitto.

Messages published to FairCom's MQTT broker are forwarded automatically to the Mosquitto broker. Mosquitto broker subscribers receive the messages sent to FairCom's MQTT broker.

Messages published to the Mosquitto broker are forwarded automatically to FairCom's MQTT broker, which publishes them to its subscribers.

This tutorial assumes you are running both brokers on the same computer, and has you configure them to use different ports.

 

Requirements:

 

Set up the Mosquitto broker

Set up the Mosquitto broker to send and receive payloads from the FairCom MQ broker:

Mosquitto and FairCom MQ must be on different ports.

In this procedure, Mosquitto will be listening on port 1884, and FairCom MQ will be on 1883. The port number does not matter as long as they are open ports. Similarly, the configuration file name does not matter.

  1. Create a configuration file inside the Mosquitto folder.
  2. Add the following to the configuration file.
    listener 1884
    allow_anonymous true
  3. Save the configuration file.
  4. Run the Mosquitto -c 1884.conf -v command to start the Mosquitto broker.

 

Configure the FairCom broker

Configure the FairCom MQ broker to forward and receive payloads from the Mosquitto broker:

In this procedure, the FairCom GUI is used to configure a broker connection and configure a topic, in that order.

  1. Navigate to the Code tab.
  2. Select API Console from the Code menu.
  3. Paste this code into the API Console and click the Run button:
    1. {
          "api": "mq",
          "apiVersion": "1.0",
          "requestId": "00000015",
          "authToken": "replaceWithAuthTokenFromCreateSession",
          "action": "configureBrokerConnection",
          "params":
        {
              "brokerConnectionName": "MosqBroker",
              "brokerHostname": "127.0.0.1",
              "brokerPort": "1884"
        }
      }
  4. Observe the response and ensure the action completed successfully.
    1. "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.
      {
        "authToken": "replaceWithAuthTokenFromCreateSession",
        "result": {},
        "requestId": "00000015",
        "debugInfo": {
          "request": {
            "authToken": "replaceWithAuthTokenFromCreateSession",
            "api": "mq",
            "action": "configureBrokerConnection",
            "params": {
              "brokerConnectionName": "MosqBroker",
              "brokerHostname": "127.0.0.1",
              "brokerPort": "1884"
            },
            "apiVersion": "1.0",
            "requestId": "00000008",
            "debug": "max"
          }
        },
        "errorCode": 0,
        "errorMessage": ""
      }
      
  5. Run the minimal example of the "listBrokerConnections" action from the JSON Actions dropdown menu.
  6. Observe the list of broker connections in the response to verify that "MosqBroker" was created.
    {
      "authToken": "replaceWithAuthTokenFromCreateSession",
      "result": {
        "brokerConnectionNames": [
          "MosqBroker"
        ]
      },
      "requestId": "00000013",
      "debugInfo": {
        "request": {
          "authToken": "replaceWithAuthTokenFromCreateSession",
          "api": "mq",
          "action": "listBrokerConnections",
          "apiVersion": "1.0",
          "requestId": "00000013",
          "debug": "max"
        }
      },
      "errorCode": 0,
      "errorMessage": ""
    }
    

 

Forward to Mosquitto

This quick tutorial shows you how to configure FairCom's MQTT broker to forward messages to a Mosquitto broker. Subscribers to topics on the Mosquitto broker automatically receive messages published to those topics on FairCom's MQTT broker.

Enable Mosquitto payloads to send to the FairCom MQ broker:
  1. Navigate to the Code tab.
  2. Select API Console from the Code menu.
  3. Paste this code into the API Console and click the Run button:
    1. The "params" object must have the inbound "topic" property. The "topic" value is the topic that will be forwarded to the external broker.
      {
        "api": "mq",
        "apiVersion": "1.0",
        "requestId": "00000001",
        "authToken": "replaceWithAuthTokenFromCreateSession",
        "action": "configureTopic",
        "params": {
          "topic": "house",
          "forwardToExternalBrokers": [
            {
              "brokerConnectionName": "MosqBroker",
              "topic": "house"
            }
          ]
        }
      }
  4. Observe the response and ensure the action completed successfully.
    1. "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.
      {
        "authToken": "replaceWithAuthTokenFromCreateSession",
        "result": {},
        "requestId": "00000001",
        "debugInfo": {
          "request": {
            "authToken": "replaceWithAuthTokenFromCreateSession",
            "api": "mq",
            "action": "configureTopic",
            "params": {
              "topic": "house",
              "forwardToExternalBrokers": [
                {
                  "brokerConnectionName": "MosqBroker",
                  "topic": "house"
                }
              ]
            },
            "apiVersion": "1.0",
            "requestId": "00000014",
            "debug": "max"
          }
        },
        "errorCode": 0,
        "errorMessage": ""
      }
      

 

Subscribe to Mosquitto

This quick tutorial shows you how to configure the FairCom MQTT broker to subscribe to topics on a Mosquitto broker. Subscribers to those topics on FairCom's MQTT broker automatically receive messages published to Mosquitto.

Enable a subscriber to receive payloads from the FairCom MQ broker:
  1. Navigate to the Code tab.
  2. Select API Console from the Code menu.
  3. Paste this code into the API Console and click the Run button:
    {
      "authToken": "replaceWithAuthTokenFromCreateSession",
      "api": "mq",
      "action": "configureTopic",
      "params": {
        "topic": "house",
        "subscribeToExternalBroker": "MosqBroker"
      }
    }
  4. Observe the response and ensure the action completed successfully.
    1. "errorCode" with a value of 0 indicates success. "errorCode" with a non-zero value indicates a failure, see Errors and contact FairCom with any questions.
      {
        "authToken": "replaceWithAuthTokenFromCreateSession",
        "result": {},
        "requestId": "00000015",
        "debugInfo": {
          "request": {
            "authToken": "replaceWithAuthTokenFromCreateSession",
            "api": "mq",
            "action": "configureTopic",
            "params": {
              "topic": "house",
              "subscribeToExternalBroker": "MosqBroker"
            },
            "requestId": "00000015",
            "debug": "max"
          }
        },
        "errorCode": 0,
        "errorMessage": ""
      }
      

 

Create a subscriber client

Create a subscriber client to receive all messages sent to the Mosquitto broker.

To connect to a FairCom driver client, follow this procedure:
  1. Navigate to the python.mqtt directory in <faircom>/drivers/python.mqtt/pythonMQTTTutorial1/subscribe.
  2. Run the command: python subscribe.py -t house -s 127.0.0.1:1884
To connect to a Mosquitto subscriber client, follow this procedure:
  1. Run the command: mosquitto_sub -h 127.0.0.1 -t house -p 1884
  2. Observe a new connection to the Mosquitto broker on which the broker is running.

 

Publish a message to the Mosquitto broker

Publish a message to the Mosquitto broker to verify that the message is forwarded to the running FairCom Edge broker, and then received by the subscriber:

This tutorial uses the python.mqtt driver. For more information on this driver, see MQTT client for Python.

  1. Navigate to the python.mqtt directory in <faircom>/drivers/python.mqtt/pythonMQTTTutorial1/publish.
  2. Run the python publish.py -t house -f data.json localhost:1884 command to publish a JSON object to port 1884.
  3. Observe that the published JSON sample in the driver package has been successfully received by the subscriber client.
  4. Alternatively, you can publish with Mosquitto using this command: mosquitto_pub -h 127.0.0.1 -t house -p 1883 -m “house temperature 21.12”