hivemq / hivemq-mqtt-client

HiveMQ MQTT Client is an MQTT 5.0 and MQTT 3.1.1 compatible and feature-rich high-performance Java client library with different API flavours and backpressure support
https://hivemq.github.io/hivemq-mqtt-client/
Apache License 2.0
832 stars 153 forks source link

Expose packetId in MqttPublish #583

Closed tmbull closed 1 year ago

tmbull commented 1 year ago

🚀 Feature Request

I would like to have access to the packetId primarily for logging/tracing purposes. Something like this maybe?

Optional<Integer> getPacketId();

🔈 Motivation

I am currently in the process of migrating from the paho mqtt3.1.1 client to the async hivemq MQTT5 client. The message type in the paho client has a getMessageId method which, I assume, represents the same data as the packetId in the HiveMQ client. We are currently using this ID for both manual acknowledgement and tracing messages between publisher and subscribers in logs.

🛰 Alternatives

An alternative would be to update our data model to include some unique identifier for messages. However, this is more work as it requires touching every message type that we might send over MQTT.

📎 Additional context

I don't have any further context. I am more or less wanting to know the feasibility of this request and if there is any reason that the packetId is not exposed aside from the fact that manual acknowledgement is handled internally via the acknowledge method on the Mqtt*Publish interface (which I appreciate).

SgtSilvio commented 1 year ago

Hi @tmbull

if there is any reason that the packetId is not exposed

Yes, the packet id is intentionally not exposed. First, it is really a packet id and not a message id. The latter suggests that it identifies a message throughout its entire lifetime and that it will be the same for the publisher and subscriber, but this is not the case. The packet id is really only valid for the transmission of the packet between client and broker. Also, the packet id is not present when you create a new message that you want to publish. It will be assigned when the client internally starts the transmission of the message. Which packet id can be assigned to a packet is defined in the MQTT specification and so needs to be assigned by the client's internals. The user should not tinker with a self-assigned packet id.

We are currently using this ID for [...] tracing messages between publisher and subscribers in logs.

As I described above, the packet id is not a message id, so can not be used to correlate messages between publisher and subscribers.

Are these explanations fine for you?

tmbull commented 1 year ago

Hi @SgtSilvio thank you for the detailed reply. Based on your explanation, I think there was probably some misunderstanding in our old code that utilized the paho client. I'm closing this issue as you've thoroughly answered my question.