简体   繁体   English

新订阅时未使用 Mosquitto 的 QoS 1 消息

[英]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.很确定这是一个问题,可能总结了我对 MQTT 的(缺乏)理解,所以提前道歉。

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.我正在使用 MQTT 在两台设备之间进行通信,并且通过使用 QoS 1,我希望我可以随时发布消息,并且我的订阅设备可以在下次连接时随时接收该消息。

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).但是,如果我不这样做,则只要新客户端订阅该主题,就会使用该消息(至少这是我对 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.不,消息不会为新客户端排队,无论您在什么 QOS 上发布它们。

High QOS messages are only queued for existing clients that have a persistent session registered with the broker.高 QOS 消息仅对已向代理注册的持久 session 的现有客户端进行排队。 This is a client with a fixed clientId and that reconnects with the clean session flag set to false.这是一个具有固定 clientId 的客户端,并且在干净的 session 标志设置为 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. mosquitto_sub命令每次运行时都会生成一个新的随机 clientId,因此它不会重新连接到代理持有的现有持久 session。 It also defaults to clean session true.它还默认清除 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.然后在发布测试消息之前断开此客户端(ctrl-c),然后再次运行mosquitto_sub命令,您应该会看到代理已为该特定客户端将所有发布到该主题的消息排队。

You can read more about high QOS message queuing and persistant sessions here您可以在此处阅读有关高 QOS 消息队列和持久会话的更多信息

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.当消息发布时将保留标志设置为 true,代理将保留给定主题的最后一个保留,并在任何客户端订阅该主题时立即交付。 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您可以在此处阅读有关保留消息的更多信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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