简体   繁体   中英

Paho-mqtt subscribe one-time message

Is there an elegant way to pull one message off the broker without having to:

  1. subscribe
  2. create an on_message()
  3. receive the message
  4. unsubscribe

I ask because we are using a json message which has multiple fields. When new data comes in I want to ONLY update that particular field in the json message but not remove the rest of the data. Since we have a TON of these json topics, we don't really want to keep all of them in program memory (also in case the program has to be relaunched). On top of that, this program could be running for months without supervision.

So ideally, I'd like to post the json message to an ID'd topic with the retain flag set to True. Then when new data comes in for that ID, I do a pull of the info on that topic, update that particular field in the json message and repost to the same topic.

I can post example code but I'm hoping there is a simple function that I am unaware of.

Thanks, in advance, for any suggestions.

The Paho Python client comes with a set of help classes that do this single shot type of pattern for you.

Doc here

eg the following connects to a broker, subscribes to a topic and returns on receipt of the first message on that topic.

import paho.mqtt.subscribe as subscribe

msg = subscribe.simple("paho/test/simple", hostname="mqtt.eclipse.org")
print("%s %s" % (msg.topic, msg.payload))

And the matching publish call:

import paho.mqtt.publish as publish

publish.single("paho/test/single", "payload", hostname="mqtt.eclipse.org")

I don't think that is possible. You say "When new data comes in..." That's exacty why you need to subscribe and use the callback function. That's basically a "pull when something is actually there".

Just to get an idea of how it should work: you are sending that json message via MQTT, right? And you are re-sending it when it changes?

But you don't have to keep them all in the RAM. You could use a retained message in combination with a fixed topic (not ID'ed) and send the ID in the message.

If you use retained messages with ID'ed topics, that might fill the memory.

What does the ID stand for? A uniqie number? Something like a timestamp? A hash? The sender?

I think you can solve that problem by clearly separating your things, eg say in data and message , where data is something you maintain in Python (eg a database or something in RAM) and message is something that you acually send / receive via MQTT.

Then you can add / send / update data depending on what is received in MQTT and you don't have to send / update the complete set.

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