# How the publishing client gets acknowledgement that the message is sent to the broker

**URL:** <https://community.hivemq.com/t/how-the-publishing-client-gets-acknowledgement-that-the-message-is-sent-to-the-broker/1441>\
**Category:** HiveMQ Client Library\
**Created:** [September 16, 2022, 11:50am UTC](https://community.hivemq.com/t/how-the-publishing-client-gets-acknowledgement-that-the-message-is-sent-to-the-broker/1441 "2022-09-16T11:50:15Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek](https://avatars.discourse-cdn.com/v4/letter/a/7ba0ec/32.png) [@Abhishek](https://community.hivemq.com/u/Abhishek)\
**Post date:** [September 16, 2022, 11:50am UTC](https://community.hivemq.com/t/how-the-publishing-client-gets-acknowledgement-that-the-message-is-sent-to-the-broker/1441/1 "2022-09-16T11:50:15Z")

</div>

I have a scenario where an mqtt-client (java application) publishes message to the mqtt-broker using the below code.

mqttClient  
.toAsync()  
.publishWith()  
.topic(“topic”)  
.payload(“message”.getBytes(StandardCharsets.UTF\_8))  
.send()  
.**whenComplete((connAck, throwable)** → {  
LOGGER.info(“Debug: connAck=” + connAck);  
});

1. Is this “whenComplete()” invoked when the message has reached the mqtt-broker? or just after publish method is invoked by the publisher-client? or broker has send the message to the subscriber-client? or after subscriber has received the message?

2. Can I consider this whenComplete() as an acknowledgement?

3. In case of successful ack → throwable is null  
In case of failure ack → throwable is not null

In either case (success/failure ack), the publishing-client has to perform certain operation (application-level operation) on the message?  
Problem:  
a. In successful ack - how to uniquely identify acknowledgement corresponding to a message? (Suppose, the publisher-client has published 100 msgs, how the publisher-client will uniquely identify for which message the acknowledge has been received?)  
b. In failure ack - the publishing-client is not getting the message, it is just getting a throwable object.

---

<div class="post-metadata">

**Author:** ![Daria\_H](https://avatars.discourse-cdn.com/v4/letter/d/dfb087/32.png) [@Daria\_H](https://community.hivemq.com/u/Daria_H)\
**Post date:** [September 21, 2022, 2:53pm UTC](https://community.hivemq.com/t/how-the-publishing-client-gets-acknowledgement-that-the-message-is-sent-to-the-broker/1441/2 "2022-09-21T14:53:36Z")

</div>

Hi @Abhishek ,

Please take a look at the example: [hivemq-mqtt-client/AsyncDemo.java at master · hivemq/hivemq-mqtt-client · GitHub](https://github.com/hivemq/hivemq-mqtt-client/blob/master/examples/src/main/java/com/hivemq/client/mqtt/examples/AsyncDemo.java) – where an async client connects to the broker, publishes a message, receives the result and disconnects.

I hope it helps,  
Dasha from HiveMQ team
