I want to connect a client which will monitor all the topics of the broker to respond to the events when I don't know what are names of topic.
4 Answers
Subscribing to #
gives you a subscription to everything except for topics that start with a $
(these are normally control topics anyway).
It is better to know what you are subscribing to first though, of course, and note that some broker configurations may disallow subscribing to #
explicitly.
-
But the following link says that we should not be subscribing to # hivemq.com/blog/…. As it adds a lot of overhead on the broker. If the number of topics are too many. Apr 10, 2017 at 4:35
-
1
-
2
-
5@ChristianBaumann That is not correct.
#
gets you everything. There is no requirement to start with/
, and I would actively encourage you not to do that - it adds an extra unnecessary level of hierarchy. If you split the topic string/one/two//three
into its elements, you get `` ,one
,two
, ``,three
. So subscribing to/#
won't receive messages published toone
, for example.– ralightMar 28, 2019 at 15:02 -
11That is because the # is being swallowed by your shell as a comment. Try
mosquitto_sub -t '#'
ormosquitto_sub -t \#
– ralightApr 2, 2019 at 10:16
You can use mosquitto_sub
(which is part of the mosquitto-clients
package) and subscribe to the wildcard topic #
:
mosquitto_sub -v -h broker_ip -p 1883 -t '#'
Concrete example
mosquitto.org is very active (at the time of this posting). This is a nice smoke test for a MQTT subscriber linux device:
mosquitto_sub -h test.mosquitto.org -t "#" -v
The "#"
is a wildcard for topics and returns all messages (topics): the server had a lot of traffic, so it returned a 'firehose' of messages.
If your MQTT device publishes a topic of irisys/V4D-19230005/
to the test MQTT broker , then you could filter the messages:
mosquitto_sub -h test.mosquitto.org -t "irisys/V4D-19230005/#" -v
Options:
-h
the hostname (default MQTT port = 1883)-t
precedes the topic
-
2
-
1@hardillb Excellent question! Rem has a fine answer, however, my example is 'concrete' in the sense that it points to a high traffic MQTT broker, so the reader can quickly / easily test from the command line. mqtt.eclipse.org does not have regular traffic and was problematic. I have tried to provide the community with a simple working 'concrete' example and avoid the pitfalls / obstacles I encountered. I feel that my answer is the next evolution of rem's fine answer (I upvoted rem's answer) Aug 29, 2019 at 21:29
Use the wildcard "#" but beware that at some point you will have to somehow understand the data passing through the bus!