TsgcWSPClient_MQTTMethods › UnSubscribe

UnSubscribe Method

Removes one or more active topic subscriptions from the current MQTT session.

Overloads

Overload 1

Syntax

function UnSubscribe(const aTopic: string; aUnsubscribeProperties: TsgcWSMQTTUnsubscribe_Properties = nil) : Word;

Parameters

NameTypeDescription
aTopicconst stringExact topic filter, matching a previous Subscribe call, to remove.
aUnsubscribePropertiesTsgcWSMQTTUnsubscribe_PropertiesOptional MQTT 5.0 properties (for example User Properties). Pass nil when not required.

Return Value

Packet identifier assigned to the UNSUBSCRIBE packet; correlate it with OnMQTTUnSubscribe to read the broker's reason codes. (Word)

Remarks

Single-topic overload. The filter must match the one originally passed to Subscribe, including wildcards, otherwise the broker will report a "No subscription existed" reason code. Pending messages already in flight may still be delivered after the call.

Example

MQTT.UnSubscribe('sensors/+/temperature');

Overload 2

Syntax

function UnSubscribe(aTopics: TsgcWSTopics): Word;

Parameters

NameTypeDescription
aTopicsTsgcWSTopicsCollection of topic filters to drop with a single UNSUBSCRIBE packet.

Return Value

Packet identifier of the batched UNSUBSCRIBE packet. (Word)

Remarks

Batch overload useful when logging off a client that tracks many filters, or when a mode change forces rewiring of several subscriptions at once. Each entry in the collection produces its own reason code in the broker's UNSUBACK, reported through OnMQTTUnSubscribe.

Example

oTopics := TsgcWSTopics.Create;
try
  oTopics.Add('sensors/#');
  oTopics.Add('alerts/#');
  MQTT.UnSubscribe(oTopics);
finally
  oTopics.Free;
end;

Back to Methods