MQTT Retain on TagoIO Broker

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

  1. 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.
  2. 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.
  1. Create an Analysis: Go to the Analysis section and create a new Analysis. Choose Node.js as the environment.
  2. Implement MQTT Publish Script: Within your Analysis, implement a script that connects to the TagoIO MQTT broker and publishes your desired topic.
  3. 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:

  1. /*
  2. ** Analysis Example
  3. ** MQTT publish
  4. **
  5. * Snippet to push data to MQTT. Follow this pattern within your application
  6. * If you want more details about MQTT, search "MQTT" in TagoIO help center.
  7. * You can find plenty of documentation about this topic.
  8. * TagoIO Team.
  9. **
  10. ** How to use?
  11. ** In order to trigger this analysis you must setup an Action to trigger this analysis
  12. */

  13. const { Analysis, Services } = require("@tago-io/sdk");

  14. async function mqttPushExample(context, scope) {
  15.  // Create your data object to push to MQTT
  16.  // In this case we're sending a JSON object.
  17.  // You can send anything you want.
  18.  // Example:
  19.  // const myDataObject = 'This is a string';
  20.  const myDataObject = {
  21.    variable: "temperature_celsius",
  22.    value: (myData.value - 32) * (5 / 9),
  23.    unit: "C",
  24.  };

  25.  // Create a object with the options you chooses
  26.  const options = {
  27.    qos: 0,
  28.  };

  29.  // Publishing to MQTT
  30.  const MQTT = new Services({ token: context.token }).MQTT;
  31.  MQTT.publish({
  32.      bucket: myData.device, // for immutable/mutable devices
  33.      message: JSON.stringify(myDataObject),
  34.      topic: "tago/my_topic",
  35.      options,
  36.    }).then(context.log, context.log)
  37. }

  38. module.exports = new Analysis(mqttPushExample);

  39. // To run analysis on your machine (external)
  40. // module.exports = new Analysis(mqttPushExample, { token: "YOUR-TOKEN" });

    • Related Articles

    • 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 ...
    • MQTT - Process data, Publish it and Subscribe to a topic

      Starting on June 1st, TagoIO will no longer provide MQTT broker to Free accounts. Read more In this tutorial, you will learn how to process data, publish to a topic, and subscribe to it. We will be using the MQTTX client throughout this tutorial. ...