redboltz / mqtt_cpp

Boost Software License 1.0
441 stars 107 forks source link

async_publish msg with qos0, but received not in order, is it possible? #981

Closed samuelyhsu closed 1 year ago

samuelyhsu commented 1 year ago

publish a qos0 message with async_publish of async_client, other client subscribed to the message may receive the message in garbled order?

redboltz commented 1 year ago

If you use mqtt_cpp client and mqtt_cpp broker, then async published publish packet (QoS0) order is preserved.

For example,

clientP1 --- mqtt_cpp broker --- clientS1
  1. clientP1 publish PUBLISH packet that is QoS0 and payload is A
  2. clientP1 publish PUBLISH packet that is QoS0 and payload is B
  3. clientP1 publish PUBLISH packet that is QoS0 and payload is C

Then clientS1 always receives payload A,B,C order.

If the underlying socket is different, order is not guaranteed.

For example,

clientP1 --- mqtt_cpp broker --- clientS1
clientP2 ---
  1. clientP1 publish PUBLISH packet that is QoS0 and payload is A
  2. clientP2 publish PUBLISH packet that is QoS0 and payload is B
  3. clientP1 publish PUBLISH packet that is QoS0 and payload is C

Then clientS1 receives ABC, ACB, or BAC, order. A is always before C, but B can interrupt anyware.

If you use non mqtt_cpp broker, then the order depends on the broker implementation.

At least, the broker receives expected order (ABC for the first example), in other words, mqtt_cpp client(endpoint) never reorder publish QoS0 packet internally.

samuelyhsu commented 1 year ago

There is only one clientP1 and clientS1, publishing and receiving are all single-threaded, but broker is not mqtt_cpp, sent three messages ABC continuously , but the receiver received ACB, may be the broker rearranged the order? I use emqx as broker https://www.emqx.io/

redboltz commented 1 year ago

I guess so.

You can try async_mqtt (mqtt_cpp's successor project) docker container broker.

https://github.com/redboltz/async_mqtt/blob/doc/README.md#run-on-the-docker-container

For testing you can replace emqx with async_mqtt broker, and observe the result.

samuelyhsu commented 1 year ago

I'll try it, thanks a lot!