简体   繁体   中英

Mosquitto's QoS 1 message not being consumed upon new subscription

Pretty sure this is a question that probably summarises my (lack of) understanding of MQTT, so apologising in advance.

I am using MQTT to communicate between two devices, and by using QoS 1 I expect that I can publish a message at any time and my subscribing device can pick that message up at any time when it next connects.

So for example, I want to send the following message:

mosquitto_pub -t switch/sign/switch -m "ahoy world!" --qos 1 -d

If I have a subscriber already subscribed to this topic, then it will consume the message (and this bit works well). But if I don't, the message will be consumed as soon as a new client subscribes to the topic (at least that's how I understand QoS).

My understanding is that I should immediately get the message by subscribing as such:

mosquitto_sub -t switch/sign/switch --qos 1 -d

When I subscribe, however, I don't get any messages.

Would appreciate some help here.

Thanks

Nope, messages are not queued for new clients, no matter what QOS you publish them at.

High QOS messages are only queued for existing clients that have a persistent session registered with the broker. This is a client with a fixed clientId and that reconnects with the clean session flag set to false.

The mosquitto_sub command will generate a fresh random clientId every time it's run so it will not reconnect to an existing persistent session held by the broker. It also defaults to clean session true.

If you run the following:

mosquitto_sub -c -i fixedClientId -t switch/sign/switch --qos 1  

Then disconnect this client (ctrl-c) before publishing your test messages and then run the mosquitto_sub command again you should see that the broker has queued any published messages to that topic for this specific client.

You can read more about high QOS message queuing and persistant sessions here

The other option is to publish a retained message. When a messages are published with the retained flag set to true the broker will hold on to the last retained on a given topic and deliver it as soon a any client subscribes to that topic. This is just one message, and it is replaced every time a message with the retained bit is set is received by the broker for that topic. You can read more about retained messages here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM