Search code examples
pythonmqttmosquitto

How to know mqtt topics without client.subscribe() in python


If a message is sent to the topic in an mqtt broker, I want to know the topic by Python. In order to use client.subscribe (), I have to manually enter a topic, so I need to know the topic before client.subscribe() dynamically. Is there a way to know what broker topics are?


Solution

  • If you send "EVERY" message to broker with retain message = True, then you can:

    1. Connects to server with subscribe '#'
    2. Check all the retain message and their topic (then you can got all topics)
    3. Unsubscribe '#'
    4. Subscribe the topic you want

    This solution subscribe twice, may not fit your original requirement (only subscribe once), but can do what you want