85

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.

Yugandhar Chaudhari
  • 3,831
  • 3
  • 24
  • 40

4 Answers4

128

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.

ralight
  • 11,033
  • 3
  • 49
  • 59
  • But the following link says that we should not be subscribing to # http://www.hivemq.com/blog/mqtt-essentials-part-5-mqtt-topics-best-practices. As it adds a lot of overhead on the broker. If the number of topics are too many. – Vishweshwar Kapse Apr 10 '17 at 04:35
  • 1
    @ralight is there any way to restrict this behavior in rabbitmq? – Suraj Aug 18 '17 at 11:45
  • 2
    Don´t forget the leading "/", so it should be "-t /#" – Christian Baumann Mar 27 '19 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 '19 at 15:02
  • @ralight I get your point, but when I execute `mosquitto_sub -d -u username -P password -t #` I get `Error: -t argument given but no topic specified.` – Christian Baumann Mar 31 '19 at 18:29
  • 12
    That is because the # is being swallowed by your shell as a comment. Try `mosquitto_sub -t '#'` or `mosquitto_sub -t \#` – ralight Apr 02 '19 at 10:16
82

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 '#'
WoJ
  • 27,165
  • 48
  • 180
  • 345
rem
  • 1,131
  • 10
  • 15
17

Concrete example with existing MQTT server

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

To publish a test message said server:

mosquitto_pub -h test.mosquitto.org  -m "$NOW,QFNONS,B6,0677,JFKCDG" -t "irisys/V4D-19230005/"

Options:

  • -h the hostname (default MQTT port = 1883)
  • -t precedes the topic

MQTT Anonymous Server Setup

After testing these concrete example or if said server is unavailable: an MQTT server can be quickly stood up on your own Ubuntu device:

sudo add-apt-repository ppa:mosquitto-dev/mosquitto-ppa
sudo apt install mosquitto mosquitto-clients

and configuring /etc/mosquitto/mosquitto.conf to enable anonymous pub/ sub

# enable anonymous traffic through port 1833  https://stackoverflow.com/a/41852506/4953146
listener 1883 #https://mosquitto.org/documentation/authentication-methods/
allow_anonymous true

without authentication for testing / proof of concept. After validating on your LAN, you can forward 1883 WAN traffic for further testing.

gatorback
  • 1,351
  • 4
  • 19
  • 44
  • 2
    Why have you just repeated an existing answer (by rem)? – hardillb Aug 29 '19 at 19:30
  • 2
    @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 '19 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!

Raúl Salinas-Monteagudo
  • 3,412
  • 1
  • 24
  • 22