Skip to main content

FairCom MQTT Tutorial: Forward to and from Mosquitto

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 (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 Mosquitto broker. Messages published to FairCom's MQTT broker engine on one port will be forwarded to the Mosquitto broker running on a different port. Mosquitto broker subscribers will then receive the messages.

Requirements:

Complete the tutorial requirements before this procedure.

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

Important

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 MQ broker to forward and receive payloads from the Mosquitto broker:

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

  1. Select the JSON 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": "MosqBroker",
            "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": "MosqBroker",
            "brokerHostname": "127.0.0.1",
            "brokerPort": "1884"
          },
          "apiVersion": "1.0",
          "requestId": "00000008",
          "debug": "max"
        }
      },
      "errorCode": 0,
      "errorMessage": ""
    }
    
  7. Select the "listBrokerConnections" action from the JSON Actions dropdown menu.

  8. Click Send request (Run Icon).

  9. Observe the list of broker connections in the response to verify that "MosqBroker" was created.

    {
      "authToken": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
      "result": {
        "brokerConnectionNames": [
          "MosqBroker"
        ]
      },
      "requestId": "00000013",
      "debugInfo": {
        "request": {
          "authToken": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
          "api": "mq",
          "action": "listBrokerConnections",
          "apiVersion": "1.0",
          "requestId": "00000013",
          "debug": "max"
        }
      },
      "errorCode": 0,
      "errorMessage": ""
    }
    
Enable Mosquitto payloads to send to the FairCom MQ broker:
  1. Select "configureTopic" from the JSON Actions dropdown menu.

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

    Note

    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": "anAuthTokenProvidedByServer",
      "action": "configureTopic",
      "params": {
        "topic": "house",
        "forwardToExternalBrokers": [
          {
            "brokerConnectionName": "MosqBroker",
            "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": "00000001",
      "debugInfo": {
        "request": {
          "authToken": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
          "api": "mq",
          "action": "configureTopic",
          "params": {
            "topic": "house",
            "forwardToExternalBrokers": [
              {
                "brokerConnectionName": "MosqBroker",
                "topic": "house"
              }
            ]
          },
          "apiVersion": "1.0",
          "requestId": "00000014",
          "debug": "max"
        }
      },
      "errorCode": 0,
      "errorMessage": ""
    }
    
Enable a subscriber to receive payloads from the FairCom MQ broker:
  1. Select "configureTopic" 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": "MosqBroker"
      },
    }
  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": "00000015",
      "debugInfo": {
        "request": {
          "authToken": "PqxlwypHN8h7KHL2yVotMjCEj7dl0MdCyFRu6LEs59bsJy6EH6XEOaOHHxKVyyOp",
          "api": "mq",
          "action": "configureTopic",
          "params": {
            "topic": "house",
            "subscribeToExternalBroker": "MosqBroker"
          },
          "requestId": "00000015",
          "debug": "max"
        }
      },
      "errorCode": 0,
      "errorMessage": ""
    }
    

Create a subscriber client that will receive all the 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 where the broker is running.

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. Run the python publish.py -t house -f data.json localhost:1884 command, to publish a JSON object to port 1884.

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

  3. 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"