MQTTClient.unsubscribe()
Overview
Unsubscribes from a topic filter on a connected MQTT broker.
Added in version 2.1.13
Format
var mqttClient = new MQTTClient(options);
//...
mqttClient.unsubscribe(subscription, onSuccess, onFailure);
Value | Description | Type | Required | Notes |
---|---|---|---|---|
subscription | An object or a string describing the subscription. | String or Object | Required | If a string than the string represents the filter. Subscription object properties are described below. |
onComplete | A callback function that is called asynchronously if the subscribe completes. | Function | Optional | When called, the first parameter is a Complete object as described below. |
onError | A callback function that is called asynchronously if the subscribe errors. | Function | Optional | When called, the first parameter is an Error object as described below. |
Subscription object properties
Value | Description | Type | Required | Notes |
---|---|---|---|---|
filter | The topic filter. | String | Required | May contain + symbol to represent a single intermediate level wildcard. May end with the # symbol to indicate a match with any further levels. |
Complete object properties
Value | Description | Type |
Notes |
---|---|---|---|
result | A result code. | Integer |
Possible values: •0 – Success •17 – No subscription existed •128 – Unspecified error •131 – Implementation specific error •135 – Not authorized •143 – Topic filter invalid •145 – Packet identifier in use |
Error object properties
Value | Description | Type |
Notes |
---|---|---|---|
message | A message that describes the failure. | String | May be undefined. |
type | A category for the type of failure. | String |
Example
Copy
/* Displays a toast when a message is received on the "velocity/popup/toast" topic.
* Unsubscribes after first toast.
*/
mqttClient = new MQTTClient({
broker: "mqtt://mymqtt.example.com",
subscribeTo: [
{
filter: "velocity/popup/toast",
qos: 0,
callback: function(message) {
View.toast(message.payloadAsString, true);
mqttClient.unsubscribe("velocity/popup/toast", function(complete) {
Logger.debug("unsubscribed: " + complete.result);
}, function(error) {
Logger.warning("failed unsubscribing: " + (error.message || error.type));
});
}
}
]
});
mqttClient.connect();