fix: ensure broker always respond with PUBCOMP to a PUBREL in QoS 2
Fixes #634
Just checked: the tests fail on QoS2 handling,which makes sense because the QoS2 message has not been published yet:
https://github.com/moscajs/aedes/blob/2c6882ef60f1b41fd5d5d2d36168eaa100afe6fd/lib/handlers/publish.js#L42-L46
What needs to happen in the publish handler (per[MQTT-4.3.3-2]) is: a) check if we already have packetID by using client.broker.persistence.incomingGetPacket() b) if not found:
- client.broker.publish() // we have taken ownership for the packet so we publish it
- if publish was successfull : client.broker.persistence.incomingStorePacket() c) always send PUBREC packet for this packet , even if we already have it in persistence
This way the same publish packet will never be published twice
Then in the PubRel handler we: a) check if we have packetID by using client.broker.persistence.incomingGetPacket() b) if found, we delete this packetID by using persistence.incomingDelPacket() c) always send a PUBCOMP packet for this packet, even if we did not have it in persistence
This releases the packetID so that it can be reused for a new PUBLISH packet.
Hope this helps!
Hans ps. further optimization would be not to store the whole packet in incomingStore, but only the packetID. since we only need to retain the packetID.