MQTT::topic

Description

This command manipulates topic(s) of MQTT messages. This command is valid only for following MQTT message types:

  • PUBLISH
  • SUBSCRIBE
  • UNSUBSCRIBE

Syntax

MQTT::topic
MQTT::topic replace <topic-name>
MQTT::topic count
MQTT::topic list
MQTT::topic index <index-number>
MQTT::topic qos <topic-name>
MQTT::topic add <topic-name> [<qos>]
MQTT::topic delete <topic-name>

MQTT::topic

  • Get the topic-name of MQTT PUBLISH message. Or the first topic of a SUBSCRIBE or UNSUBSCRIBE message.

MQTT::topic replace <topic-name>

  • Set the topic-name of MQTT PUBLISH message to specified string This command is valid only for PUBLISH messages.

MQTT::topic count

  • Get the number of topics in MQTT SUBSCRIBE and UNSUBSCRIBE messages.

MQTT::topic list

  • Get the list of topics in MQTT SUBSCRIBE and UNSUBSCRIBE messages.

MQTT::topic index <index-number>

  • Get the topics at index in the list of topics in MQTT SUBSCRIBE and UNSUBSCRIBE messages.

MQTT::topic qos <topic-name>

  • This command is valid for SUBSCRIBE messages only. It returns the QoS value of the given topic-name.

MQTT::topic add <topic-name> [<qos>]

  • For SUBSCRIBE messages, add the specified topic name and qos to the end of list of topics. Qos can only be 0,1, or 2. If not specified QoS 0 will be used. For UNSUBSCRIBE messages, add the specified topic name to the end of list of topics. UNSUBSCRIBE messages do not need QoS. If supplied, it will be ignored.

MQTT::topic delete
  • Delete the specified topic name and its qos from the topics in MQTT SUBSCRIBE and UNSUBSCRIBE messages.

Examples

Automatically assign a subscription to the client for topic “$SYS/broker/alerts/critical”:

when MQTT_CLIENT_INGRESS {
    set cmtype [MQTT::type]
    if { $cmtype == "SUBSCRIBE" } {
        set topic_count [MQTT::topic count]
        set mid [MQTT::packet_id]
        set countmap($mid) $topic_count
        MQTT::topic add "$SYS/broker/alerts/critical" 0
    }
}
when MQTT_SERVER_INGRESS {
    set smtype [MQTT::type]
    if {$smtype == "SUBACK"} {
       set mid [MQTT::packet_id]
       set tc countmap($mid)
       set return_codes [MQTT::return_code_list]
       set return_codes [lreplace $return_codes $tc $tc]
       MQTT::replace type SUBACK packet_id $mid return_code_list $return_codes
    }
}