Skip to main content

FairCom MQTT Tutorial: Forward to and from HiveMQ

Note

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, factories, and clouds. (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 detailed instructions for configuring FairCom's MQTT broker engine to forward messages to a HiveMQ broker. Messages published to FairCom's MQTT broker engine on one port will be forwarded to the HiveMQ broker running on a different port. HiveMQ broker subscribers will then receive the messages.

Requirements:

Complete the tutorial requirements before this procedure.

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

Important

HiveMQ and FairCom MQ must be on different ports.

The default port for both FairCom MQ and HiveMQ is 1883.

In this procedure, HiveMQ 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. Navigate to the configure folder typically named conf, in the unpacked HiveMQ folder.

  2. Locate and open the Config XML file inside the conf folder with your preferred text editor.

  3. Verify the listening port is <port>1884</port>.

  4. Verify that anonymous-usage-statistics is set to true.

    <anonymous-usage-statistics>
            <enabled>true</enabled>
     </anonymous-usage-statistics>
    
  5. Start the HiveMQ broker.

    1. Navigate back to the parent directory where the bin folder is located.

    2. Run the run.bat file.

Configure the FairCom MQ broker to forward and receive a newly started HiveMQ broker:

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

  1. Select MQ API from the Select API dropdown menu.

  2. Select the "configureBrokerConnection" action from the JSON Actions dropdown menu.

  3. Replace the JSON in the API Request editor with the following JSON:

    {
      "api": "mq",
      "apiVersion": "1.0",
      "requestId": "00000015",
      "authToken": "aAuthTokenProvidedByTheServer",
      "action": "configureBrokerConnection",
      "params": {
        "brokerConnectionName": "HiveMQConnection",
        "brokerHostname": "127.0.0.1",
        "brokerPort": "1884"
      }
    }
  4. Click Apply defaults to JSON request (Apply.PNG) to replace the "authToken" value with the valid one from your session.

  5. Click Send request (Run Icon).

  6. Observe the response and ensure the action completed successfully.

    Note

    "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": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
      "result": {},
      "requestId": "00000015",
      "debugInfo": {
        "request": {
          "authToken": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
          "api": "mq",
          "action": "configureBrokerConnection",
          "params": {
            "brokerConnectionName": "HiveMQConnection",
            "brokerHostname": "127.0.0.1",
            "brokerPort": "1884"
          },
          "apiVersion": "1.0",
          "requestId": "00000016",
          "debug": "max"
        }
      },
      "errorCode": 0,
      "errorMessage": ""
    }
    
  7. Select the "listBrokerConnection" action from the JSON Actions dropdown menu.

  8. Click Send request (Run Icon).

  9. Observe the list of "brokerConnectionNames" in the response to verify "HiveMQConnection" was created.

    {
      "authToken": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
      "result": {
        "brokerConnectionNames": [
          "HiveMQConnection",
          "MosqBroker"
        ]
      },
      "requestId": "00000018",
      "debugInfo": {
        "request": {
          "authToken": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
          "api": "mq",
          "action": "listBrokerConnections",
          "apiVersion": "1.0",
          "requestId": "00000018",
          "debug": "max"
        }
      },
      "errorCode": 0,
      "errorMessage": ""
    }
    
  1. Select the "configureTopic" action from the JSON Actions dropdown menu.

  2. Replace the JSON in the API Request editor with the following JSON:

    Note

    • The "params.topic" value is the MQTT topic that receives inbound messages.

    • The "params.forwardToExternalBroker.[].topic" value is the topic on the target broker that will receive the MQTT topic messages.

    {
      "api": "mq",
      "apiVersion": "1.0",
      "requestId": "00000001",
      "authToken": "anAuthTokenProvidedByServer",
      "action": "configureTopic",
      "params": {
        "topic": "house",
        "forwardToExternalBroker": [
          {
            "brokerConnectionName": "HiveMQConnection",
            "topic": "house"
          }
        ]
      }
    }
  3. Click Apply defaults to JSON request (Apply.PNG) to replace the "authToken" value with the valid one from your session.

  4. Click Send request (Run Icon).

  5. Observe the response and ensure the action completed successfully.

    Note

    "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": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
      "result": {},
      "requestId": "00000019",
      "debugInfo": {
        "request": {
          "authToken": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
          "api": "mq",
          "action": "configureTopic",
          "params": {
            "topic": "house",
            "forwardToExternalBroker": [
              {
                "brokerConnectionName": "HiveMQConnection",
                "topic": "house"
              }
            ]
          },
          "apiVersion": "1.0",
          "requestId": "00000019",
          "debug": "max"
        }
      },
      "errorCode": 0,
      "errorMessage": ""
    }
    
Enable a subscriber to receive payloads from the FairCom MQ broker:
  1. Select the "configureTopic" action from the JSON Actions dropdown menu.

  2. Replace the JSON in the API Request editor with the following JSON:

    {
      "authToken": "authTokenHere",
      "api": "mq",
      "action": "configureTopic",
      "params": {
        "topic": "house",
        "subscribeToExternalBroker": "HiveMQConnection"
      }
    }
  3. Click Apply defaults to JSON request (Apply.PNG) to replace the "authToken" value with the valid one from your session.

  4. Click Send request (Run Icon).

  5. Observe the response and ensure the action completed successfully.

    Note

    "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": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
      "result": {},
      "requestId": "00000020",
      "debugInfo": {
        "request": {
          "authToken": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
          "api": "mq",
          "action": "configureTopic",
          "params": {
            "topic": "house",
            "subscribeToExternalBroker": "HiveMQConnection"
          },
          "requestId": "00000020",
          "debug": "max"
        }
      },
      "errorCode": 0,
      "errorMessage": ""
    }
    
Create a subscriber client that will receive all the messages a HiveMQ broker receives:

This procedure will subscribe you to all messages on a topic house, on port 1884, the port that HiveMQ is listening on.

  1. Navigate to the python.mqtt directory in <faircom>/drivers/python.mqtt/PythonMQTTTutorial1/subscribe to use one of FairCom's drivers.

  2. Run the python subscribe.py -t house -s 127.0.0.1:1884 command.

Publish a message to the FairCom MQ broker, to verify that the message is forwarded to the running HiveMQ 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 and open the python.mqtt directory through <faircom>/drivers/python.mqtt/PythonMQTTTutorial1/publish to use one of FairCom's drivers.

  2. Run the python publish.py -t house -f data.json command, to publish a JSON object to port 1883.

  3. Observe that the published JSON sample, contained in the driver package, has been successfully received by the HiveMQ subscriber client.