MQTT Retain on TagoIO Broker
Starting on June 1st, TagoIO will no longer provide MQTT broker to Free accounts. Read more
TagoIO's MQTT broker, while not designed to support the native retain feature found in standard MQTT protocol implementations, we offer a workaround to achieve similar functionality.
This approach involves utilizing Analysis and Actions features to store the last message published to a topic and then resend it when a new client subscribes.
Setting Up Message on Subscribing
To build an MQTT Retain workaround with TagoIO, you can either publish directly to a topic with an Action or use an Analysis script for more complex scenarios.
Using Actions to Publish a Message to a Subscriber
- Create an Action: Navigate to the Actions section in your TagoIO dashboard and create a new action. Choose the event that will trigger this action. It could be a variable update, a scheduled time, or another custom trigger.
- Configure MQTT Publish: In the action configuration, select "MQTT Publish" as the action type. You must specify the topic you wish to publish to and the message payload.
Using Analysis for Advanced Scenarios
For scenarios where is needed to publish for a large number of devices/topics and also you need more control over the publishing process, you can use an Analysis script.
- Create an Analysis: Go to the Analysis section and create a new Analysis. Choose Node.js as the environment.
- Implement MQTT Publish Script: Within your Analysis, implement a script that connects to the TagoIO MQTT broker and publishes your desired topic.
- Trigger Analysis: Configure an Action or another event to trigger the execution of your Analysis. This could be based on a device update, a scheduled time, or any other event in your TagoIO application.
See an Analysis Script to MQTT Publish:
- /*
- ** Analysis Example
- ** MQTT publish
- **
- * Snippet to push data to MQTT. Follow this pattern within your application
- * If you want more details about MQTT, search "MQTT" in TagoIO help center.
- * You can find plenty of documentation about this topic.
- * TagoIO Team.
- **
- ** How to use?
- ** In order to trigger this analysis you must setup an Action to trigger this analysis
- */
- const { Analysis, Services } = require("@tago-io/sdk");
- async function mqttPushExample(context, scope) {
- // Create your data object to push to MQTT
- // In this case we're sending a JSON object.
- // You can send anything you want.
- // Example:
- // const myDataObject = 'This is a string';
- const myDataObject = {
- variable: "temperature_celsius",
- value: (myData.value - 32) * (5 / 9),
- unit: "C",
- };
- // Create a object with the options you chooses
- const options = {
- qos: 0,
- };
- // Publishing to MQTT
- const MQTT = new Services({ token: context.token }).MQTT;
- MQTT.publish({
- bucket: myData.device, // for immutable/mutable devices
- message: JSON.stringify(myDataObject),
- topic: "tago/my_topic",
- options,
- }).then(context.log, context.log)
- }
- module.exports = new Analysis(mqttPushExample);
- // To run analysis on your machine (external)
- // module.exports = new Analysis(mqttPushExample, { token: "YOUR-TOKEN" });
Related Articles
Connecting your MQTT Broker to TagoIO
TagoIO supports MQTT connections through our MQTT Relay command-line tool. This software acts as a bridge between your MQTT Broker and the TagoIO platform, facilitating seamless integration and data flow. Developed in Rust, the TagoIO MQTT Relay is ...
MQTT
Starting on June 1st, TagoIO will no longer provide MQTT broker to Free accounts. Read more MQTT stands for MQ Telemetry Transport; it's an extremely simple and lightweight publish-subscribe messaging protocol. It was designed for constrained devices ...
MQTT - Publishing and Subscribing
Starting on June 1st, TagoIO will no longer provide MQTT broker to Free accounts. Read more You can publish to your MQTT topics by coding a script that will run from an Analysis. When the analysis runs, your script can publish a topic that can be ...
MQTT with Sensor Tag
This is an example using the SensorTag Bluetooth module from Texas Instruments to send data to TagoIO. There is no code modification needed in the SensorTag side, and as it uses the MQTT protocol, only a configuration setup is needed. For the ...
Trigger by MQTT Topic
The trigger type, Trigger by Variable, allows you to execute your Action when data is sent to an MQTT topic. TagoIO has its own MQTT broker that is responsible for pushing data to clients in case something new is published in the specific topics they ...