Yugandhar Chaudhari
Yugandhar Chaudhari

Reputation: 3984

How do I subscribe to all topics of a MQTT broker

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.

Upvotes: 98

Views: 248657

Answers (4)

gatorback
gatorback

Reputation: 1547

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.

Upvotes: 21

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

Upvotes: 2

rem
rem

Reputation: 1291

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 '#'

Upvotes: 94

ralight
ralight

Reputation: 11628

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.

Upvotes: 143

Related Questions