Skip to main content

mqttclient.unsubscribe(topics, callback)

Stop subscribing to one or more MQTT topics

Availability

Agent

Parameters

Name Type Description
topics String or blob, or array of strings/blobs Topic(s) to be unsubscribed. Each must be valid UTF-8
callback Function Called when the broker responds to the unsubscribe request

Returns

Nothing

Description

This method allows you to end a subscription to one or more topics.

The callback function is executed when the agent receives an unsubscribe acknowledgement from the MQTT broker. It has one parameter, resultCode, into which an integer response code will be passed:

Value Description
0 Successfully unsubscribed
-1 The unsubscription attempt failed

If you provide multiple topics, you will only receive the successful unsubscribe code if all of the topics were unsubscribed. If at least one topic’s unsubscribe attempt fails, you will receive a failure notification, even if all the other topic unsubscribe actions succeeded.

An exception will be thrown if you call unsubscribe() on an mqttclient instance that is currently not connected. An exception will also be thrown if you include more than ten topics in an array passed into topics.

We recommend that you wrap all mqttclient.unsubscribe() calls in a try... catch statement. Even if the client is connected at the time of the call, the socket could nonetheless fail as a result of the send process itself.

Note Inbound and Outbound MQTT requests and messages are subject to rate limits.