subscribe

suspend fun subscribe(topicFilter: String, qos: QoS = QoS.AT_MOST_ONCE, noLocal: Boolean = false, retainAsPublished: Boolean = false, retainHandling: RetainHandling = RetainHandling.SEND_AT_SUBSCRIBE)

Subscribe to a single topic filter with the specified qos and MQTT 5.0 subscription options.

Throws MqttException.SubscriptionRefused if the broker rejects the filter, so a caller is never left believing it is subscribed to something it will hear nothing on.

Parameters

topicFilter

The MQTT topic filter (e.g. "sensors/#").

qos

Maximum QoS level for messages on this subscription.

noLocal

If true, the server will not forward messages published by this client (§3.8.3.1).

retainAsPublished

If true, retain flag from original publish is preserved (§3.8.3.1).

retainHandling

Controls when retained messages are sent (§3.8.3.1).

Throws

if the topic filter is invalid (§4.7).

if not connected.


suspend fun subscribe(topicFilters: Map<String, QoS>)

Subscribe to multiple topic filters with per-topic QoS levels.

Throws MqttException.SubscriptionRefused if the broker rejects any of the filters; the ones it granted are subscribed, and the exception names both sets.

Parameters

topicFilters

Map of topic filter to maximum QoS level.

Throws

if any topic filter is invalid (§4.7).

if not connected.


suspend fun subscribe(subscriptions: List<Subscription>)

Subscribe to multiple topic filters with full subscription options.

Unlike subscribe with Map<String, QoS>, this overload preserves all per-topic MQTT 5.0 subscription options (noLocal, retainAsPublished, retainHandling).

Parameters

subscriptions

List of Subscription objects with per-topic options.

Throws

if any topic filter is invalid (§4.7).

if not connected.