Reputation: 43
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?
P.S. I'm using MQTT.JS
Upvotes: 2
Views: 10212
Reputation: 59638
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/
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/
Upvotes: 5