All Products
Search
Document Center

ApsaraMQ for MQTT:Subscription options

Last Updated:Aug 23, 2024

ApsaraMQ for MQTT supports the subscription options feature of Message Queuing Telemetry Transport (MQTT) 5.0. This topic describes the subscription options feature.

Feature description

To facilitate the flexible management of topic subscriptions, subscription options are introduced into MQTT 5.0. You can use subscription options to specify the quality of service (QoS) for messages and whether a device can receive messages that are published by itself. This enables clients to manage message streams and message reception in a fine-grained manner.

The following table describes the subscription options supported by MQTT 5.0.

Subscription option

Description

QoS level

The QoS level of messages in MQTT.

No Local

Specifies whether a device can receive messages that are published by itself.

QoS level

The following table describes the QoS levels.

Level

Description

Scenario

QoS 0

A message can be received at most once. Message delivery is not reliable and the message may be lost.

Sensor data transmission.

QoS 1

A message can be received at least once. Message delivery is reliable.

Remote control commands.

QoS 2

A message can be received only once.

None.

No Local

If a device that uses MQTT 3.1.1 subscribes to a topic to which the device publishes messages, the device can receive messages that are published by itself. If the device uses MQTT 5.0, you can set the No Local option to 1. This way, the device cannot receive messages that are published by itself.

Valid values:

  • 1: The device cannot receive messages that are published by itself.

  • 0: The device can receive messages that are published by itself.

Scenarios

  • Service quality assurance: You can specify the QoS levels of messages in different topics. This way, messages can be delivered based on specific requirements.

  • Message routing optimization: You can specify different subscription options to optimize the routing and scheduling of messages, especially messages in large-scale distributed systems.

  • Resource optimization: You can change subscription options for clients with limited bandwidth or computing resources to reduce the number of unnecessary messages that the clients receive.

Example

A client wants to receive only newly produced messages whose QoS level is lower than or equal to the QoS level of messages that the client can process from a specific topic.

In this case, you can specify the subscription options in the SUBSCRIBE packet of the client to receive only messages whose QoS level is 0. Sample code:

SUBSCRIBE
Topic Filter: sensors/temperature
Subscription Options:
  QoS: 0
  No Local: true
  Retain As Published: false
  Retain Handling: 0

The following items describe the subscription options for the preceding example:

  • QoS: The value is set to 0, which indicates that the broker sends only messages whose QoS level is 0 to the client.

  • No Local: The value is set to true, which indicates that the client cannot receive messages that are published by itself.

Subscription options allow you to manage topic subscriptions on clients based on your business requirements and resource restrictions. This makes MQTT 5.0 more flexible and adaptable to various scenarios and requirements.