Skip to content

Commit b8b7bc3

Browse files
authored
manual puback (#852)
1 parent 0591327 commit b8b7bc3

4 files changed

Lines changed: 84 additions & 1 deletion

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The primary purpose of the AWS IoT Device SDK for C++ v2 is to simplify the proc
2424
* Integrated service clients for AWS IoT Core services
2525
* Secure device connections to AWS IoT Core using MQTT protocol including MQTT 5.0
2626
* Support for [multiple authentication methods and connection types](./documents/MQTT5_Userguide.md#connecting-to-aws-iot-core)
27+
* Support for [manual publish acknowledgement](./documents/MQTT5_Userguide.md#manual-publish-acknowledgement) for control over QoS 1 PUBACK delivery
2728

2829
#### Supported AWS IoT Core services
2930

documents/FAQ.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* [What certificates do I need?](#what-certificates-do-i-need)
1515
* [Where can I find MQTT 311 Samples?](#where-can-i-find-mqtt-311-samples)
1616
* [Certificate and Private Key Usage Across Different Versions of the SDK on macOS](#certificate-and-private-key-usage-across-different-versions-of-the-sdk-on-macos)
17+
* [Manual Publish Acknowledgement and QoS 1 Redelivery](#manual-publish-acknowledgement-and-qos-1-redelivery)
1718
* [I still have more questions about this sdk?](#i-still-have-more-questions-about-this-sdk)
1819

1920
### Where should I start?
@@ -180,6 +181,18 @@ The MQTT 3.1.1 samples can be found in the v1.40.0 samples folder [here](https:/
180181
### Certificate and Private Key Usage Across Different Versions of the SDK on macOS
181182
A certificate and private key pair cannot be shared on a macOS device between aws-iot-device-sdk-cpp-v2 v1.41.0 and any other versions. In the update to v1.41.0 we migrated macOS from using Apple's deprecated Security Framework to SecItem API. In doing so, certificate and private keys are imported in a non-backwards compatible manner into the Apple Keychain.
182183

184+
### Manual Publish Acknowledgement and QoS 1 Redelivery
185+
186+
When using [manual publish acknowledgement](./MQTT5_Userguide.md#manual-publish-acknowledgement), there are two important behaviors to be aware of regarding QoS 1 message redelivery:
187+
188+
**Broker redelivery of unacknowledged publishes**
189+
190+
The AWS IoT broker will periodically resend unacknowledged QoS 1 PUBLISH packets. These redeliveries should be treated as duplicates even if the DUP flag in the PUBLISH packet is not set. If the manual publish acknowledgement is not acquired again for a redelivered packet, the acknowledgement will be sent automatically.
191+
192+
**Session resumption after disconnect/reconnect**
193+
194+
Upon a disconnect and reconnect of the MQTT5 client, if a session is resumed, any previously acquired acknowledgement handle is void. The broker will resend the unacknowledged PUBLISH packet, and the acknowledgement must be reacquired from that resent packet. If the resent packet is not handled for manual acknowledgement, the acknowledgement will be sent automatically.
195+
183196
### I still have more questions about this SDK?
184197

185198
* [Here](https://docs.aws.amazon.com/iot/latest/developerguide/what-is-aws-iot.html) are the AWS IoT Core docs for more details about IoT Core

documents/MQTT5_Userguide.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
- [Subscribe](#subscribe)
2525
- [Unsubscribe](#unsubscribe)
2626
- [Publish](#publish)
27+
* [Advanced Operations and Settings](#advanced-operations-and-settings)
28+
+ [Manual Publish Acknowledgement](#manual-publish-acknowledgement)
2729
* [MQTT5 Best Practices](#mqtt5-best-practices)
2830

2931
# Introduction
@@ -757,6 +759,73 @@ If the PUBLISH was a QoS 1 publish, then the completion callback returns a PubAc
757759
```
758760
759761
762+
## Advanced Operations and Settings
763+
764+
### Manual Publish Acknowledgement
765+
766+
By default, the MQTT5 client automatically sends a PUBACK for every QoS 1 PUBLISH it receives, immediately after the `OnPublishReceivedHandler` callback returns. Manual publish acknowledgement gives you control over when that PUBACK is sent, allowing you to defer acknowledgement until after your application has fully processed the message — for example, after persisting it to a database or forwarding it to another service.
767+
768+
To take manual control of the PUBACK, call `eventData.acquirePublishAcknowledgement()` **within** the `OnPublishReceivedHandler` callback. This returns a `ScopedResource<PublishAcknowledgementHandle>` that you can store and use later to send the PUBACK by calling `client->InvokePublishAcknowledgement()`.
769+
770+
**Important constraints:**
771+
* `acquirePublishAcknowledgement()` must be called within the `OnPublishReceivedHandler` callback. Calling it after the callback returns or from a different thread will return `nullptr`.
772+
* `acquirePublishAcknowledgement()` may only be called once per received PUBLISH. Subsequent calls return `nullptr`.
773+
* This is only relevant for QoS 1 messages. For QoS 0 messages, `acquirePublishAcknowledgement()` returns `nullptr`.
774+
* If `acquirePublishAcknowledgement()` is not called (or returns `nullptr`), the client will automatically send the PUBACK when the callback returns.
775+
776+
The following example shows how to acquire the acknowledgement handle within the callback and invoke it later:
777+
778+
```cpp
779+
// A shared location to store the acknowledgement handle for later use
780+
Crt::ScopedResource<Mqtt5::PublishAcknowledgementHandle> pendingAck;
781+
782+
// Set the publish received callback on the builder
783+
builder->WithPublishReceivedCallback(
784+
[&pendingAck](const Mqtt5::PublishReceivedEventData &eventData) {
785+
if (eventData.publishPacket == nullptr)
786+
return;
787+
788+
fprintf(stdout, "Publish received on topic %s\n",
789+
eventData.publishPacket->getTopic().c_str());
790+
791+
// Acquire manual control of the PUBACK for this QoS 1 message.
792+
// This must be called within the callback. After the callback returns,
793+
// acquirePublishAcknowledgement() will return nullptr.
794+
pendingAck = eventData.acquirePublishAcknowledgement();
795+
796+
if (pendingAck == nullptr)
797+
{
798+
// QoS 0 message or acknowledgement already taken — nothing to do.
799+
return;
800+
}
801+
802+
// The PUBACK will NOT be sent automatically because we acquired the handle.
803+
});
804+
805+
std::shared_ptr<Aws::Crt::Mqtt5Client> client = builder->Build();
806+
807+
// ... connect, subscribe, and receive messages ...
808+
809+
// After processing is complete, send the PUBACK by invoking the acknowledgement.
810+
if (pendingAck != nullptr)
811+
{
812+
if (!client->InvokePublishAcknowledgement(*pendingAck))
813+
{
814+
fprintf(stdout, "Failed to invoke publish acknowledgement.\n");
815+
}
816+
}
817+
818+
```
819+
820+
**AWS IoT broker redelivery behavior**
821+
822+
The AWS IoT broker will periodically resend unacknowledged QoS 1 PUBLISH packets. These redeliveries should be treated as duplicates even if the DUP flag in the PUBLISH packet is not set. If `acquirePublishAcknowledgement()` is not called again for a redelivered packet, the acknowledgement will be sent automatically.
823+
824+
**Session resumption after disconnect/reconnect**
825+
826+
Upon a disconnect and reconnect of the MQTT5 client, if a session is resumed, any previously acquired `ScopedResource<PublishAcknowledgementHandle>` is void. The broker will resend the unacknowledged PUBLISH packet, and `acquirePublishAcknowledgement()` must be called again within the callback for that resent packet. If the resent packet is not handled for manual acknowledgement, the acknowledgement will be sent automatically.
827+
828+
760829
# MQTT5 Best Practices
761830

762831
Below are some best practices for the MQTT5 client that are recommended to follow for the best development experience:

0 commit comments

Comments
 (0)