问题
I am having mosquitto broker running on Ubuntu on EC2. Also I did a change max_queued_messages 1000
and max_inflight_messages 10
in /etc/mosquitto/mosquitto.conf
I am publishing to this broker with qos=2 and retain enabled. For publishing I am using mosquitto_pub -t 1 -m "{c:{d:\"pN\",m:\"Pr1\"}}" -r -q 2
. And subscriber side I am using org.eclipse.paho.android.service:1.0.2
library with same qos=2.
and only providing single option by options.setMqttVersion(MqttConnectOptions.MQTT_VERSION_3_1)
for parameters to client.connect(options)
.
Problem is coming if subscriber device connected to the server disconnects and reconnects again due to any reason, then getting only single last retained message, it is missing the all in between messages.
And also if nothing even published yet, still after reconnecting gets the last retained message which subscriber device might had already got before disconnecting.
I want that after reconnecting subscriber should get all messages which were published in the period of disconnection and if already got then should not shown again. Wanted to know how and where to do it.
回答1:
The last retained message published will always be delivered when you subscribe to a topic even if it has already been delivered before no mater what QOS level. This is intentional and there is no way to stop it if the message is published as retained. (Just to be clear, publishing a message with the retained flag will replace the last retained message and does not queue up all retained messages.)
As for not getting the messages published between a disconnect/reconnect this implies that the client it passing the cleansession=true
connection option. If you want to have the missed messages delivered then you need to ensure the cleansession
flag is set to false
at connection time. This is totally independent of if messages are published with the retained flag.
来源:https://stackoverflow.com/questions/36289471/mosquitto-broker-retain-multiple-messages