All Products
Search
Document Center

ApsaraMQ for MQTT:Optimized device-cloud interactions

Last Updated:Apr 08, 2025

ApsaraMQ for MQTT supports optimized device-cloud interactions in Message Queuing Telemetry Transport (MQTT) 5.0. This topic describes the optimized device-cloud interactions.

Feature description

Multiple features, such as the request-response pattern, subscription options, and improved error reports, are introduced into MQTT 5.0 to optimize the interactions between clients and servers (cloud). These features are designed to improve the flexibility, reliability, and persistence of communications and provide more effective solutions for device-cloud scenarios.

Scenarios

  • Request-response interactions: You can initiate a request on a client to query data or perform an operation on the server and then receive a response from the server. This feature works in a manner similar to the traditional HTTP request-response pattern.

  • Quality of service (QoS) management: You can use subscription options to specify the maximum QoS level on a client and specify whether to receive retained messages to ensure service quality.

  • Status synchronization: The message retention and session expiration features help recover session status after clients are reconnected. This improves the persistence of communication.

Connection interaction

Connection interaction enables the negotiation between devices and servers. When a client establishes a connection with a server, the configured information about the client is sent to the server to confirm whether the connection is successful. If the connection is successful, the server returns the final values of the configuration items.

Parameter

Description

Value returned from the server

Wildcard Subscription Available

  • 1: Wildcard subscriptions are supported.

  • 0: Wildcard subscriptions are not supported.

1

Subscription Identifiers Available

  • 1: Subscription identifiers are supported.

  • 0: Subscription identifiers are not supported.

1

Topic Alias

  • 1: The server accepts topic aliases on the connection.

  • 0: The server does not accept topic aliases on the connection.

1

Maximum QoS

  • 2: QoS 2.

  • 1: QoS 1.

  • 0: QoS 0.

2

Server Keep Alive

The server continues to send heartbeat packets or data packets during the keep-alive period. Otherwise, the connection is disrupted. This parameter specifies the keep-alive period.

Valid values: 1 to 8.

Unit: minutes.

The retention period that the device specifies in the connection parameters is returned.

If the returned value exceeds 8 minutes, 8 minutes is used.

Session Expiry Interval

The retention period of sessions after the connection is disrupted.

Valid values: 0 to 2147483647.

Unit: seconds.

The retention period that the device specifies in the connection parameters is returned.

If this parameter is not configured, the default value 0 is returned. The value 0 indicates that sessions expire immediately after the connection is interrupted. If this parameter is configured, a value that indicates 10 minutes is returned. If the value is less than 10 minutes, 10 minutes is used.

Maximum Packet Size

Valid values: 0 to 128.

Unit: KB.

Note
  • The maximum size of a message body in a data packet is 64 KB.

  • After the server receives a data packet, the server does not check the size of the data packet. Instead, the data packet is directly pushed to the server. If you want the server to check the size of data packets and intercept data packets whose size exceeds the maximum specified value, join the DingTalk group 116015007918 to contact ApsaraMQ for MQTT technical support.

A fixed value of 131072 is returned. The value indicates 128 KB.

Topic Alias Maximum

Valid values: 0 to 30.

  • Up to 30 topic aliases are supported by the server in each connection. If the number of topic aliases in messages sent by the client to the server exceeds 30, the connection is closed and a topic name error is reported.

  • The client can declare up to 30 supported topic aliases to the server in each connection. If the number of declared topic aliases exceeds 30, topic aliases are not specified when the server pushes messages to the client.

Receive Maximum

The Receive Maximum property is added to MQTT 5.0. This property is available in CONNECT and CONNACK packets. You can use the Receive Maximum property to specify the maximum number of messages with QoS 1 or 2 that a client or a server can process at the same time. The maximum value of this property is 65535. If the specified value exceeds the maximum value, 65535 is used.

The Receive Maximum property can be used to limit the maximum number of messages with QoS 1 or 2 that the peer client or server can send. If the number of unpublished messages in the peer client or server is equal to the specified value of the Receive Maximum property, messages whose QoS is 1 or 2 can no longer be sent. This helps implement traffic throttling.

Active server disconnection

In MQTT 5.0, the server can close the connection to a device and send a DISCONNECT packet to the device. The DISCONNECT packet contains an error code and a string that explains why the connection was closed. The DISCONNECT packet simplifies troubleshooting.

Common error codes

Decimal

Hexadecimal

Name

Packet type

0

0x00

Success

CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, UNSUBACK, and AUTH

0

0x00

Normal disconnection

DISCONNECT

0

0x00

Granted QoS 0

SUBACK

1

0x01

Granted QoS 1

SUBACK

2

0x02

Granted QoS 2

SUBACK

4

0x04

Disconnect with Will Message

DISCONNECT

17

0x11

No subscription existed

UNSUBACK

128

0x80

Unspecified error

CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, and DISCONNECT

129

0x81

Malformed Packet

CONNACK and DISCONNECT

130

0x82

Protocol Error

CONNACK and DISCONNECT

131

0x83

Implementation specific error

CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, and DISCONNECT

132

0x84

Unsupported Protocol Version

CONNACK

133

0x85

Client Identifier not valid

CONNACK

134

0x86

Bad User Name or Password

CONNACK

135

0x87

Not authorized

CONNACK, PUBACK, PUBREC, SUBACK, UNSUBACK, and DISCONNECT

136

0x88

Server unavailable

CONNACK

137

0x89

Server busy

CONNACK and DISCONNECT

138

0x8A

Banned

CONNACK

140

0x8C

Bad authentication method

CONNACK and DISCONNECT

141

0x8D

Keep Alive timeout

DISCONNECT

142

0x8E

Session taken over

DISCONNECT

143

0x8F

Topic Filter invalid

SUBACK, UNSUBACK, and DISCONNECT

144

0x90

Topic Name invalid

CONNACK, PUBACK, PUBREC, and DISCONNECT

145

0x91

Packet Identifier in use

PUBACK, PUBREC, SUBACK, and UNSUBACK

146

0x92

Packet Identifier not found

PUBREL and PUBCOMP

147

0x93

Receive Maximum exceeded

DISCONNECT

148

0x94

Topic Alias invalid

DISCONNECT

149

0x95

Packet too large

CONNACK and DISCONNECT

150

0x96

Message rate too high

DISCONNECT

151

0x97

Quota exceeded

CONNACK, PUBACK, PUBREC, SUBACK, and DISCONNECT

153

0x99

Payload format invalid

CONNACK, PUBACK, PUBREC, and DISCONNECT

154

0x9A

Retain not supported

CONNACK and DISCONNECT

155

0x9B

QoS not supported

CONNACK and DISCONNECT

158

0x9E

Shared Subscription not supported

SUBACK and DISCONNECT

159

0x9F

Connection rate exceeded

CONNACK and DISCONNECT

160

0xA0

Maximum connect time

DISCONNECT

161

0xA1

Subscription Identifiers not supported

SUBACK and DISCONNECT

162

0xA2

Wildcard Subscriptions not supported

SUBACK and DISCONNECT