Component Libraries •
Project Templates
Docs •
Install •
Tutorials •
Developer Guides •
Contribute •
Blog •
Discord
Xircuits Component Library for MQTT. Easily connect, subscribe, and publish using MQTT in Xircuits workflows.
This library provides the necessary components to work with MQTT in your Xircuits workflows. You can easily connect to an MQTT broker, subscribe to topics, and publish messages within your Xircuits pipelines.
Before you begin, you will need the following:
- Python3.9+.
- Xircuits.
- MQTT Broker
Establishes a connection to an MQTT broker using the provided credentials and stores the client instance in the context.
Publishes a message to a specified topic on the MQTT broker and returns the result status.
Subscribes to a specified topic and triggers an action when a message is received.
Starts the MQTT client loop to handle incoming messages and maintain the connection.
Stops the MQTT client loop and disconnects from the MQTT broker.
To use the xai-mqtt library, you'll need to have an MQTT broker installed on your device. One popular option is Mosquitto, an open-source message broker that implements the MQTT protocol.
-
Update your package lists and install Mosquitto:
sudo apt-get update sudo apt-get install -y mosquitto mosquitto-clients
-
Enable Mosquitto to start on boot and then start the service:
sudo systemctl enable mosquitto sudo systemctl start mosquitto
-
Verify that Mosquitto is running by checking its status:
sudo systemctl status mosquitto
-
Download the Mosquitto installer from the official Mosquitto website.
-
Run the installer and follow the instructions to complete the setup.
-
Once installed, open a terminal and start the Mosquitto broker:
mosquitto
-
You can also install it as a service during the installation process to ensure it runs automatically.
-
Install Mosquitto via Homebrew:
brew install mosquitto
-
Start the Mosquitto service:
brew services start mosquitto
-
Verify that Mosquitto is running:
brew services list
To use this component library, ensure that you have an existing Xircuits setup. You can then install the MQTT library using the component library interface, or through the CLI using:
xircuits install mqtt
You can also install it manually by cloning the repository and installing it:
# base Xircuits directory
git clone https://github.com/XpressAI/xai-mqtt xai_components/xai_mqtt
pip install -r xai_components/xai_mqtt/requirements.txt
Once installed, you can start using the xai-mqtt component library to connect, subscribe, and publish to an MQTT broker in your Xircuits workflows.
Before testing Mosquitto, you need to run the mqtt_sample.xircuits
example workflow provided in the library. This workflow demonstrates how to connect, subscribe, and publish messages using the MQTT components in Xircuits.
Once you have run the example, you can manually test Mosquitto using the following steps:
After running the example workflow, you can manually test Mosquitto using the following steps:
-
Subscribe to a topic by running this command in one terminal:
mosquitto_sub -h localhost -p 1883 -t "testing_reply"
-
Publish a message by running this command in another terminal:
mosquitto_pub -h localhost -p 1883 -t "testing" -m "anyword"
The output in the subscriber terminal will be the message sent in the publisher command. If you publish
"anyword"
, the result will display:anyword!!!
- Use the
MQTTConnect
component to connect to the MQTT broker by providing the broker address. - Add the
MQTTSubscribe
component to your Xircuits diagram and specify the topic you want to subscribe to. - Connect the components you want to use to process the messages to the
on_message
triangle of theMQTTSubscribe
component. - Finally, add the
MQTTStartLoop
component to start processing. Execution will not continue past theMQTTStartLoop
component as it enters a loop to process incoming messages.
- Use the
MQTTPublish
component to publish messages to a specified topic. - Specify the topic and message content in the
MQTTPublish
component, and the message will be sent immediately.
These components allow you to fully integrate MQTT messaging within your Xircuits workflows, providing an easy way to connect, subscribe, and publish to MQTT topics.