MessageAck Event
Fired when an incoming or outgoing message has completed all acknowledgment steps.
Syntax
public event OnMessageAckHandler OnMessageAck; public delegate void OnMessageAckHandler(object sender, MqttMessageAckEventArgs e); public class MqttMessageAckEventArgs : EventArgs { public int PacketId { get; } public int Direction { get; } public int Index { get; } public int ResponseCode { get; } }
Public Event OnMessageAck As OnMessageAckHandler Public Delegate Sub OnMessageAckHandler(sender As Object, e As MqttMessageAckEventArgs) Public Class MqttMessageAckEventArgs Inherits EventArgs Public ReadOnly Property PacketId As Integer Public ReadOnly Property Direction As Integer Public ReadOnly Property Index As Integer Public ReadOnly Property ResponseCode As Integer End Class
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 IncomingMessages collection or OutgoingMessages collection.
- 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 IncomingMessages when the component receives the PUBLISH packet.
- The component sends a PUBACK (publish acknowledgment) packet in response.
- The MessageAck event is fired.
- The message is removed from IncomingMessages.
- The MessageIn event is fired.
Incoming messages with a QoS of 2 follow these steps:
- The message is added to IncomingMessages when the component receives the PUBLISH packet.
- The component sends a PUBREC (publish received) packet in response.
- The component waits to receive a PUBREL (publish release) packet.
- The component sends a PUBCOMP (publish complete) packet in response.
- The MessageAck event is fired.
- The message is removed from IncomingMessages.
- The MessageIn event is fired.
Outbound Message Processing
Outgoing messages with a QoS of 1 follow these steps:
- The component sends the PUBLISH packet, then adds the message to OutgoingMessages.
- The component waits to receive a PUBACK (publish acknowledgment) packet.
- The MessageAck event is fired.
- The message is removed from OutgoingMessages.
- The MessageOut event is fired.
Outgoing messages with a QoS of 2 follow these steps:
- The component sends the PUBLISH packet, then adds the message to OutgoingMessages.
- The component waits to receive a PUBREC (publish received) packet.
- The component sends a PUBREL (publish release) packet in response.
- The component waits to receive a PUBCOMP (publish complete) packet.
- The MessageAck event is fired.
- The message is removed from OutgoingMessages.
- 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 component 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.