MessageAck Event
Fired when an incoming or outgoing message has completed all acknowledgment steps.
Syntax
mqtt.on('MessageAck', listener: (e: {readonly packetId: number, readonly direction: number, readonly index: number, readonly responseCode: number}) => void )
Remarks
The MessageAck event fires once an incoming or outgoing message with a QoS of 1 or 2 has successfully completed all acknowledgment steps as required by its QoS level. (Note that this event does not fire for messages with a QoS of 0 since they do not require acknowledgment.)
- PacketId: The Id of the original PUBLISH packet for the message.
- Direction: Shows whether the client (0) or the server (1) is sending the data.
- Index: The index at which the message resides in either the IncomingMessage* properties or OutgoingMessage* properties.
- ResponseCode: In MQTT 5, all response packets contain Reason Codes. This argument contains any code encountered in message acknowledgment packets (PUBACK, PUBREC, PUBREL, PUBCOMP).
Possible MQTT-specific values for ResponseCode are:
Value | Applicable packets | Description |
0 | PUBACK, PUBREC, PUBREL, PUBCOMP | Success |
16 | PUBACK, PUBREC | No matching subscribers |
128 | PUBACK, PUBREC | Unspecified error |
131 | PUBACK, PUBREC | Implementation specific error |
135 | PUBACK, PUBREC | Not authorized |
144 | PUBACK, PUBREC | Topic Name invalid |
145 | PUBACK, PUBREC | Packet Identifier in use |
146 | PUBREL, PUBCOMP | Packet Identifier not found |
Inbound Message Processing
Incoming messages with a QoS of 1 follow these steps:
- The message is added to IncomingMessage* when the class receives the PUBLISH packet.
- The class sends a PUBACK (publish acknowledgment) packet in response.
- The MessageAck event is fired.
- The message is removed from IncomingMessage*.
- The MessageIn event is fired.
Incoming messages with a QoS of 2 follow these steps:
- The message is added to IncomingMessage* when the class receives the PUBLISH packet.
- The class sends a PUBREC (publish received) packet in response.
- The class waits to receive a PUBREL (publish release) packet.
- The class sends a PUBCOMP (publish complete) packet in response.
- The MessageAck event is fired.
- The message is removed from IncomingMessage*.
- The MessageIn event is fired.
Outbound Message Processing
Outgoing messages with a QoS of 1 follow these steps:
- The class sends the PUBLISH packet, then adds the message to OutgoingMessage*.
- The class waits to receive a PUBACK (publish acknowledgment) packet.
- The MessageAck event is fired.
- The message is removed from OutgoingMessage*.
- The MessageOut event is fired.
Outgoing messages with a QoS of 2 follow these steps:
- The class sends the PUBLISH packet, then adds the message to OutgoingMessage*.
- The class waits to receive a PUBREC (publish received) packet.
- The class sends a PUBREL (publish release) packet in response.
- The class waits to receive a PUBCOMP (publish complete) packet.
- The MessageAck event is fired.
- The message is removed from OutgoingMessage*.
- The MessageOut event is fired.
In MQTT 3.1.1, the RepublishInterval configuration setting, if set to a non-zero value (default), controls how long the class will wait to receive a PUBACK (for QoS 1) or PUBREC (for QoS 2) before automatically republishing an outgoing message. In MQTT 5, messages are only republished if the client is disconnected before receiving a PUBACK or PUBREC.