MQTT Reason Code Introduction and Quick Reference
Tags: mqtt
Published: 2023-06-25 04:44:35


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g22q9npp8mb088nvi2dw.png)


## Reason Code

The primary purpose of Reason Code in [**MQTT**](https://www.emqx.com/en/blog/the-easiest-guide-to-getting-started-with-mqtt) is to provide more detailed feedback to the client and server. For example, we can feed back the Reason Code corresponding to the wrong username or password to the client in the CONNACK packet, so that the client can know why it cannot connect.

### Reason Code in MQTT 3.1.1

Although MQTT 3.1.1 already supports Reason Code, it doesn't define many available Reason Codes. In fact, they are very limited.

Among the two MQTT packets that support Reason Code, the CONNACK packet has only 5 Reason Codes to indicate failures. And the SUBACK packet has only one Reason Code for indicating failure, it cannot even provide further details about the reason for the subscription failure. Moreover, for operations such as publishing and unsubscribing that don't support Reason Code, we can't even determine whether the operation was successful or not. This makes development debugging unfriendly and hinders the implementation of robust code.

### Reason Code in MQTT 5.0

So In MQTT 5.0, the number of available Reason Codes has been expanded to 43, and it is specified that Reason Codes below 0x80 are used to indicate success, while Reason Codes greater than or equal to 0x80 are used to indicate failure. Unlike MQTT 3.1.1, where Reason Codes below 0x80 could also indicate failure. This allows clients to determine whether an operation was successful or not quickly.

Furthermore, MQTT 5.0 extends the support for Reason Codes to the following packets: CONNACK, PUBACK, PUBREC, PUBREL, PUBCOMP, SUBACK, UNSUBACK, DISCONNECT, and AUTH. Now, we can not only determine if a message publication was successful but also understand the reasons for failure, such as the absence of matching subscribers or insufficient privileges to publish to a specific topic.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqp18x4qq0uuc11wo0n8.png)

Most MQTT packets include only a single Reason Code, except for SUBACK and UNSUBACK. This is because the SUBSCRIBE and UNSUBSCRIBE packets can contain multiple topic filters, and each topic filter must have a corresponding Reason Code to indicate the result of the operation. Therefore, SUBACK and UNSUBACK packets need to accommodate multiple Reason Codes. That's why Reason Codes in other packets are located in the Variable Header, while the Reason Codes in SUBACK and UNSUBACK are located in the Payload.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w3000q7nkhv6eb444ar5.png)



In this article's final Reason Code quick reference table, we have provided detailed explanations and use cases for each Reason Code in MQTT 5.0. You can take a look at it for more information.

#### Indicates to the client why the connection was disconnected

In MQTT 3.1 and 3.1.1, the DISCONNECT packet could only be sent by the client. Therefore, when a client violated certain restrictions, the server could only directly close the network connection without providing additional information to the client. This made it challenging to investigate the reason for the disconnection.

Now in MQTT 5.0, the server can send a DISCONNECT packet to the client before closing the network connection. The client can use the Reason Code in the DISCONNECT packet to get why the connection was disconnected, such as the packet being too large, the server being busy, and so on.

## Reason String

Reason String is a complement to Reason Code in MQTT 5.0, providing a human-readable string designed for diagnostic purposes. While Reason Code can indicate most error reasons, developers or operators may still need more intuitive contextual information.

For example, when the server indicates an invalid topic filter to the client using the Reason Code (0x8F), developers still have no specific information about the reason. Is it due to exceeding the maximum number of topic levels or including characters not accepted by the server? However, if the server can return a Reason String similar to "The number of topic levels exceeds the limit, the maximum is 10," developers can quickly understand the reason and make adjustments.

In practical use, the content of the Reason String depends on the specific implementation of the client and server. Therefore, a correctly implemented receiving end should not attempt to parse the content of the Reason String. Recommended usage includes but is not limited to using the Reason String when throwing exceptions or writing it to logs.

Finally, Reason String is an optional feature, and whether a Reason String is received depends on whether the peer supports it.

## MQTT 5.0 Reason Code Quick Reference Table


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jxng895fhmgwamhnf77j.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/92ah87qn0iba7nnzzc1x.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/37dst0wtlleb85mmqnl8.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/23ww83ao54sao84b9eyc.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3t84xrx40bhr8yr84q5w.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qf2ny1plntdcqh0ojriq.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9wufi1cwjvvyc59pw2b8.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/n1bqfrfgkzlkcx4n8ifa.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qwhhs0m2qwkodzl4wymc.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ows3uok44ae5q2oy9xi.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mai88149q9x2csrhjcac.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5g21ezmkbntyc0mvojha.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/94740c6g4piqootydkll.png)


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9r60d8kjh88iknndk1w5.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c5i2wljqkzhsshrqlas8.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ijog7gbmychhl7sd5d92.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/atwakbfrind9af8lo1jt.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9slnshx49iy30vl0hki1.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/urvanxh9u578og5fkiq3.png)

Originally published at [www.emqx.com](https://www.emqx.com/en/blog/mqtt5-new-features-reason-code-and-ack)