I think we can just use publish and pubrcv to meet the QoS2.
- ClientA-> Server publish packet
- Server -> ClientA pubrecv packet
When the server recv the publish packet, save to db, then the server publish to other clients, eg. ClientB. Even if we recv two same publish packets from ClientA, the server check the db, and know this is repeated message, so not publish to the ClientB.
So I don't think need 4 packets.
Do my logic is correct?
There protocol uses the two exchanges of packets in order to provide the exactly-once semantics of QoS 2 messaging.
When the server receives the
PUBLISH
it stores the ID and forwards the message on. When the server receives thePUBREL
it can then delete the ID.If the connection breaks at
*1
, the client does not know if the server received the message or not. It resends thePUBLISH
(containing the full message payload). If the server had already received the message it just needs to respond with thePUBREC
.If the connection breaks at
*2
, the client may or may not have received thePUBREC
. If it didn't, it will resend thePUBLISH
. Otherwise it will send thePUBREL
.If the connection breaks at
*3
, the client does not know if the server received the message or not. It resends thePUBREL
- which does not contain full message payload.If the connection breaks at
*4
and the client hasn't received thePUBCOMP
it can resend thePUBREL
.There are two observations for why the two exchanges are needed:
*1
. Given the protocol is intended to minimise network traffic, this is an important feature.