81

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 4

127

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.

6
  • 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
    @ralight is there any way to restrict this behavior in rabbitmq?
    – Suraj
    Aug 18, 2017 at 11:45
  • 2
    Don´t forget the leading "/", so it should be "-t /#" Mar 27, 2019 at 11:36
  • 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 to one, for example.
    – ralight
    Mar 28, 2019 at 15:02
  • 11
    That is because the # is being swallowed by your shell as a comment. Try mosquitto_sub -t '#' or mosquitto_sub -t \#
    – ralight
    Apr 2, 2019 at 10:16
79

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 '#'
1
  • Thanks man! Nobody ever shows this simple but fundamental command!
    – Avio
    Jan 20, 2019 at 21:35
14

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
  • 2
    Why have you just repeated an existing answer (by rem)?
    – hardillb
    Aug 29, 2019 at 19:30
  • 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)
    – gatorback
    Aug 29, 2019 at 21:29
2

Use the wildcard "#" but beware that at some point you will have to somehow understand the data passing through the bus!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.