RetryMQTT.subscribe()

Overview

Subscribe to a filter immediately if possible. Added to a list of subscriptions to maintain and reconnect if needed.

Subscribed with QOS 0 (At Most Once) using a clean MQTT session. Any unretained messages sent while the client is disconnected will be lost.

Subscribing to the same filter will replace any existing subscription on that same filter.

Added in version 2.1.20

Format

var retryMqtt = new RetryMQTT(options);

//...

retryMqtt.subscribe(filter, callback);

Value Description Type Required Notes
filter The topic filter. String Required May contain the + symbol to represent a single intermediate level wildcard. May end with the # symbol indicating a match with any further levels.
callback A callback function that is called when a message matching this filter is received. Function Optional The onMessage callback for the MQTTClient is always called first if it exists, followed by any matching filter subscriptions specified here. When called, the first parameter is a Message object as described in MQTTClient.subscribe().

Example

Copy
/* Subscribe to a filter. Decode the binary data as a 32 bit unsigned integer, and log it.
 */

retryMqtt = new RetryMQTT({
    broker: "mqtt://mymqtt.example.com"
});

retryMqtt.subscribe("this/+/+/test", function (message) {
    var uintBuffer = new Uint32Array(message.payload);
    var firstInt = uintBuffer[0];
    Logger.info("got the number: " + firstInt);
});