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:
- Ensure FairCom MQ is downloaded and installed.
- Ensure Mosquitto is downloaded and installed.
Set up the Mosquitto 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.
- Create a
configurationfile inside theMosquittofolder. - Add the following to the
configurationfile.listener 1884 allow_anonymous true - Save the
configurationfile. - Run the Mosquitto -c 1884.conf -v command to start the Mosquitto broker.
Configure the FairCom broker
In this procedure, the FairCom GUI is used to configure a broker connection and configure a topic, in that order.
- Navigate to the Code tab.
- Select API Console from the Code menu.
- Paste this code into the API Console and click the Run button:
{ "api": "mq", "apiVersion": "1.0", "requestId": "00000015", "authToken": "replaceWithAuthTokenFromCreateSession", "action": "configureBrokerConnection", "params": { "brokerConnectionName": "MosqBroker", "brokerHostname": "127.0.0.1", "brokerPort": "1884" } }
- Observe the response and ensure the action completed successfully.
-
"errorCode"with a value of0indicates 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": "" }
-
- Run the minimal example of the
"listBrokerConnections"action from the JSON Actions dropdown menu. - 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.
- Navigate to the Code tab.
- Select API Console from the Code menu.
- Paste this code into the API Console and click the Run button:
- 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" } ] } }
- The
- Observe the response and ensure the action completed successfully.
-
"errorCode"with a value of0indicates 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.
- Navigate to the Code tab.
- Select API Console from the Code menu.
- Paste this code into the API Console and click the Run button:
{ "authToken": "replaceWithAuthTokenFromCreateSession", "api": "mq", "action": "configureTopic", "params": { "topic": "house", "subscribeToExternalBroker": "MosqBroker" } } - Observe the response and ensure the action completed successfully.
-
"errorCode"with a value of0indicates 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.
- Navigate to the
python.mqttdirectory in<faircom>/drivers/python.mqtt/pythonMQTTTutorial1/subscribe. - Run the command: python subscribe.py -t house -s 127.0.0.1:1884
- Run the command: mosquitto_sub -h 127.0.0.1 -t house -p 1884
- Observe a new connection to the Mosquitto broker on which the broker is running.
Publish a message to the Mosquitto broker
This tutorial uses the python.mqtt driver. For more information on this driver, see MQTT client for Python.
-
Navigate to the
python.mqttdirectory in<faircom>/drivers/python.mqtt/pythonMQTTTutorial1/publish. - Run the python publish.py -t house -f data.json localhost:1884 command to publish a JSON object to port
1884. - Observe that the published JSON sample in the driver package has been successfully received by the subscriber client.
- 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”