Forward to and from Mosquitto
Tutorial to configure FairCom's MQTT broker engine to forward messages to and from a Mosquitto broker
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.
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.
Ensure FairCom MQ is downloaded and installed.
Ensure Mosquitto is downloaded and installed.
Complete the tutorial requirements before this procedure.
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.
Create a
configuration
file inside theMosquitto
folder.Add the following to the
configuration
file.listener 1884 allow_anonymous true
Save the
configuration
file.Run the Mosquitto -c 1884.conf -v command to start the Mosquitto broker.
In this procedure, the FairCom Data Explorer is used to configure a broker connection and configure a topic, in that order.
Select the JSON MQ API from the Select API dropdown menu.
Select the
"configureBrokerConnection"
action from the JSON Actions dropdown menu.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" } }
Click Apply defaults to JSON request (
) to replace the
"authToken"
value with the valid one from your session.Click Send request (
).
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
indicates success."errorCode"
with a non-zero value indicates a failure. See Errors and contact FairCom for more information about an error.{ "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": "" }
Select the
"listBrokerConnections"
action from the JSON Actions dropdown menu.Click Send request (
).
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": "" }
Select
"configureTopic"
from the JSON Actions dropdown menu.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" } ] } }
Click Apply defaults to JSON request (
) to replace the
"authToken"
value with the valid one from your session.Click Send request (
).
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
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": "" }
Select
"configureTopic"
from the JSON Actions dropdown menu.Replace the JSON in the API Request editor with the following JSON:
{ "authToken": "authTokenHere", "api": "mq", "action": "configureTopic", "params": { "topic": "house", "subscribeToExternalBroker": "MosqBroker" }, }
Click Apply defaults to JSON request (
) to replace the
"authToken"
value with the valid one from your session.Click Send request (
).
Observe the response and ensure the action completed successfully.
Note
"errorCode"
with a value of0
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.
Navigate to the
python.mqtt
directory 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 where the broker is running.
This tutorial uses the python.mqtt
driver. For more information on this driver, see MQTT client for Python.
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, contained 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"