RetryMQTT constructor
Overview
Constructor for RetryMQTT objects. The RetryMQTT object is a wrapper for MQTTClient and provides automatic reconnect and queuing for outgoing messages.
For information about using the MQTT standard, see MQTT.org or the MQTT 3.1.1 Specification.
Added in version 2.1.20
Format
var RetryMqtt = new RetryMQTT(options);
Parameter | Description | Type | Required |
---|---|---|---|
options | An options object with properties used for configuration of this MQTTClient object. Properties are listed below. | Object | Required |
Options object properties
The RetryMQTT constructor uses most of the same options for the object properties that are available for MQTTClient. The onDisconnect handler should not be set for RetryMQTT, though, because this is used by RetryMQTT to do reconnects. Additional options are listed below.
Value | Description | Type | Required |
Notes |
---|---|---|---|---|
maxQueueLength |
The maximum number of outstanding publish messages before messages begin to be discarded. |
Integer | Optional |
Must be greater than or equal to 500. Default value is 1000. |
reconnectDelay |
The delay in milliseconds between reconnect attempts. |
Integer | Optional | Must be greater than 1000. Default value is 30000. |
Example 1
/* Displays a toast when a message is received on the "velocity/toast" topic.
* Sends a message on first connect to the to "velocity/connect" topic with the Velocity ID.
* Sounds a beep when a message is received on "velocity/beep" with a payload including
* frequency and duration.
* Attempts to reconnect if disconnected.
*/
retryMqtt = new RetryMQTT({ broker: "mqtt://mymqtt.example.com" });
retryMqtt.publish("velocity/connect", Device.getVelocityId());
retryMqtt.subscribe("velocity/toast", function(message) {
View.toast(message.payloadAsString, true);
});
retryMqtt.subscribe("velocity/beep", function(message) {
var values = message.payloadAsString.split(',');
var frequency = values[0] || 2000;
var duration = values[1] || 500;
Device.beep(frequency, duration, 50);
});
Example 2
/* Publishes the last scan with a timestamp to a topic named "scan/" followed by the
* device's serial number or the Velocity ID if the serial number cannot be acquired.
* Uses a secure (TLS) connection.
* Uses the username and password specified.
* Uses a communications timeout of 30 seconds.
* Attempts to reconnect every 10 seconds when disconnected.
* Only queues up to 700 outstanding scan messages.
*/
retryMqtt = new RetryMQTT({
broker: "mqtts://mysecuremqtt.example.com",
username: "user1",
password: "secret1",
timeout: 30,
maxQueueLength: 700,
reconnectDelay: 10000
});
WLEvent.on("Scan", function(event) {
retryMqtt.publish(
"scan/" + (Device.getDeviceSerial() || Device.getVelocityId()),
JSON.stringify({timestamp: new Date(), scan: event.data})
);
}););