简体   繁体   中英

How to check if topic exists in MQTT?

I'm using mosquitto MQ with eclipse paho java library. I would like to check if topic exists or not before i subscribe to a topic and get the message. How do i do that ?

The short answer is you don't.

Topics are not something that really exist until the moment a message is published to one.

A subscriber tells the broker what topics it is interested in and should a publisher publish a message to one of those topics then the message will be forwarded to that subscriber (acls allowing).

Subscriptions can be made to topics that contain wild cards which can help if you want to match more than just a single topic. There are 2 wild card characters.

  • + which matches a single element in a topic. eg foo/+/bar will match foo/1/bar and foo/something/bar

  • # which matches multiple segments but only at the end of a topic. eg /foo/# will match /foo/1 and foo/bar/1/something. You can not place this anywhere but at the end so foo/#/bar will not work

The $SYS topics the @ΦXocę 웃 Пepeúpa ツ mentioned only supply stats about the broker not what topics exist.

There is nothing like browse topics in the broker...

the most you can get is subscribing to $sys but it will depend on some brokers config to allow you to that

the doc about $Sys information is:

  • $SYS/broker/load/bytes/received : The total number of bytes received since the broker started.

  • $SYS/broker/load/bytes/sent : The total number of bytes sent since the broker started.

  • $SYS/broker/clients/connected : The number of currently connected clients

  • $SYS/broker/clients/disconnected : The total number of persistent clients (with clean session disabled) that are registered at the broker but are currently disconnected.

  • $SYS/broker/clients/maximum : The maximum number of active clients that have been connected to the broker. This is only calculated when the $SYS topic tree is updated, so short lived client connections may not be counted.

  • $SYS/broker/clients/total : The total number of connected and disconnected clients with a persistent session currently connected and registered on the broker.

  • $SYS/broker/messages/received : The total number of messages of any type received since the broker started.

  • $SYS/broker/messages/sent : The total number of messages of any type sent since the broker started.

  • $SYS/broker/messages/publish/dropped : The total number of publish messages that have been dropped due to inflight/queuing limits.

  • $SYS/broker/messages/publish/received : The total number of PUBLISH messages received since the broker started.

  • $SYS/broker/messages/publish/sent : The total number of PUBLISH messages sent since the broker started.

  • $SYS/broker/messages/retained/count : The total number of retained messages active on the broker.

  • $SYS/broker/subscriptions/count : The total number of subscriptions active on the broker.

  • $SYS/broker/time : The current time on the server.

  • $SYS/broker/uptime : The amount of time in seconds the broker has been online.

  • $SyS/broker/version : The version of the broker. Static.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM