MQTT Example(3.0)

This section contains one example for the MQTT agent.

 

MQTT workflow example


In the above example, the MQTT agent sends any of these 3 UDR types: Error, PublishAck or SubscribeResponse to the Analysis agent, which contains the following code:

consume {
    debug(input);
    if(instanceOf(input, MQTT.SubscribeResponse)){
        MQTT.SubscribeResponse resp = (MQTT.SubscribeResponse)input;
        MQTT.Publish pub = udrCreate(MQTT.Publish);   
        pub.qos = 1;
        pub.retain = true;
        pub.topic = "test/";
        pub.data = resp.data;
        udrRoute(pub);
    } 
}

With this code, the Analysis agent will:

  • Debug the output of the MQTT agent.
  • If the received UDR is of the SubscriberResponse type, the UDR will be populated into a SubscribeResponse type UDR called resp.
  • Create a UDR of Publish type called pub.
  • Set the qos to 1, to indicate that the message will be publish with the At Least once QoS.
  • Set retain to true.
  • Set the topic to test/
  • Populate the Data with the Data field from the resp UDR.
  • Routes the pub UDR back to the MQTT agent.