dotnet / MQTTnet

MQTTnet is a high performance .NET library for MQTT based communication. It provides a MQTT client and a MQTT server (broker). The implementation is based on the documentation from http://mqtt.org/.
MIT License
4.5k stars 1.07k forks source link

Enhancements for the MqttPacketIdentifierProvider #2064

Open Nukta101 opened 3 months ago

Nukta101 commented 3 months ago

Describe the feature request

The scenario is to store the messages with packet identifier for QoS>0 on the filesystem before publishing them to the MQTT Broker. Storing the messages on the filesystem helps to survive the system/mqtt client crashes.

Currently the MQTTNet does not expose any method to set the PacketIdentifier directly in the MqttApplicationMessage and the PacketIdentifier is only available in the response of PublishAsync method.

In the reconnection scenario, Due to the Reset() method from MqttPacketIdentifierProvider being called in the ConnectAsync() the packet identifier is reset to 1, if there are messages already stored in the filesystem from the last connection that will then cause the packet id conflict.

Which project is your feature request related to?

Describe the solution you'd like

Following are possible solutions.

  1. Introducing Get property in the MqttPacketIdentifierProvider so that the current value could be read.
  2. Making the call to Reset() method optional in ConnectAsync() method.

Another approach for the point 1 would be to make it possible to directly assign the packet identifier in the MqttApplicationMessage and create the interface for MqttPacketIdentifierProvider so user can provide custom packet identifier provider.

chkr1011 commented 3 months ago

I prefer to expose the packet identifier in the MqttApplicationMessage. If it is left 0 a new one will be created using the identifier provider. Additionally, I will add a new parameter to the client options which allows to setup the offset of the identifier values when connecting with the server. The client will also get a new property which exposes (read only) the latest used packet identifier so that you can store it after disconnecting and reuse it when connecting. What do you think?

Nukta101 commented 2 months ago

Yes, that covers the use case. Thank you!