简体   繁体   中英

How to get all topics in MQTT?

I have two questions:

1) How do I get a list of all the topics and perform certain actions in the browser, depending on whether a particular topic is open or not?

2) I join a broker and subscribe to his topics. But it updates the data once a minute. Respectively, the data in the browser is updated once a minute (I go to the page and see the old data, which is updated within a minute). Is it possible to somehow force the broker to return the data to me immediately after the subscription?

PS I'm using MQTT.JS

  1. No, because topics are effectively ephemeral, they only exist at the point a client publishes to them. At that point the broker looks down the list of topic patterns that all the connected clients have requested and sends that message to those that match. The closest you can do is subscribe to the topic pattern # which will mean you see all messages (but only when they are published). # is a global wildcard that will match multiple levels (it must always go at hte end of a topic pattern and can only match whole levels), there is also the + wildcard that matches a single level in a topic. This blog post explains how wildcards work in more detail: https://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices/

  2. What you are looking for is called retained messages. Retained messages have a flag in the message header. When the broker sees this flag it will cache this message and deliver it at the point a new client subscribes to that topic before any other messages. This flag can only be set by the publisher at the point the message is published. The following blog post explains retained messages: https://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages/

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