At some point, I need to view the history on a specific topic 'topicName'. How can i do this in python?
CodePudding user response:
The history will not be saved automatically by paho-mqtt
. You will have to add each incoming MQTT message to a history object. Like this:
import paho.mqtt.client as mqtt
history = []
def on_message(client, userdata, message):
history.append(message))
client = mqtt.Client()
client.on_message = on_message
client.connect(ADDRESS, PORT, 60)
client.subscribe(TOPIC, 0)
client.loop_forever()
Then at some point you can view the history data. You can also clear the history with history = []
. I hope this guides you in the right direction.
CodePudding user response:
Short answer: You don't
(The MQTT protocol requires the broker to only stores messages for known clients that are currently offline and have requested high QOS subscription, otherwise it doesn't keep any state, and client libraries won't keep history for you)
Longer answer:
The only way to see the history of messages published to a topic is to set a client up to subscribe to that topic and store them somewhere (e.g. in a database) and then query that store. (Some brokers have plugin support that can do this)