5

I am new to MQTT protocol. As I read through the document, I couldn't see any function to remove the published topic. My purpose is to allow the publisher to remove the published topic. Did I miss something in the mqtt document? Any suggestion? Thanks !

hardillb
  • 54,545
  • 11
  • 67
  • 105
Sirapat
  • 446
  • 2
  • 8
  • 13
  • What do you mean by remove a topic, they are just addresses that messages are delivered to when published, there is nothing to remove. Or do you mean to remove a message with the retained bit set? – hardillb Feb 14 '17 at 07:36
  • I mean cancel the existed topic(address). For example, I published a topic weather/humidity with some messages. The subscriber subscribes this topic and receives the messages. I want to cancel this topic by the client. At this state, the subscriber still subscribing this topic but find out that this topic is gone. Thus, he receives no message. – Sirapat Feb 14 '17 at 08:06

3 Answers3

12

Well, if you mean remove a topic from mosquitto so that is doesn't show when you sub #, you can use:

mosquitto_pub -h <hostname>  -p <port> -u <user> -P <password> -t '<topic you want to remove>' -n -r
JeffL
  • 151
  • 1
  • 6
  • That is how to remove retained messages, which is not what the question is about. – hardillb Aug 06 '19 at 21:32
  • True, but when I searched the topic 8mo ago, this was the only thread that came up and figured it might help someone else looking for similar information. – JeffL Aug 07 '19 at 22:09
  • @hardillb Didn't realize the difference between published and retained at the time. Saw your answer about removing the persistence file on a retained thread I'll try too because the above doesn't always work on my system. Thanks! You da man! :) – JeffL Aug 07 '19 at 22:25
5

There is no concept of removing a topic.

If the publisher stops publishing data on a topic the subscribers will stop receiving data on that topic but there is nothing to remove. A subscriber can subscribe to a topic that no messages have ever been published on and that is fine, the broker will send then any messages that may be sent in the future.

Pub/sub messaging topics are not like message queues that need to be defined up front

hardillb
  • 54,545
  • 11
  • 67
  • 105
  • Thank you so much. How about If I had a local broker and there are too many topics, is it make sense if I wanna remove it? – Sirapat Feb 14 '17 at 08:46
  • 1
    As I said there is no concept of removing a topic. Topics have virtually no overhead unless the broker is actively processing a message. If you mean queued high QOS messages for offline subscribers then that is broker dependent and most of the implementations that clean them up do so on a time based expirary basis. – hardillb Feb 14 '17 at 09:02
  • Take a look at this link for removing stored messages http://www.hivemq.com/blog/mqtt-essentials-part-8-retained-messages (as answerer says, it is HiveMQ dependent...) – Goufalite Feb 14 '17 at 09:04
  • Retained messages are something different again, you publish a message with an empty payload to the topic to clear a retained message – hardillb Feb 14 '17 at 09:05
  • I see. Thank you for those informative stuffs. I'll try to understand more about it. – Sirapat Feb 14 '17 at 09:14
  • 1
    Please do NOT pay attention to @Goufalite comment, retained messages is a completely different feature and has nothing to do with topics, his comment brings confussion – Adirio Feb 14 '17 at 16:15
0

If you want an easy way to do that, with a GUI, I'd suggest to install MQTTUI. It's a lightweight GUI to see topics and the flow of MQTT messages in your terminal. It's available on Github.

Using MQTTUI, you can simply select the topic whose messages you want to remove, then type Del. It will prompt you with a confirmation dialog box to confirm you want to clean retained topics.

In the background it most probably does what @JeffL suggested in his reply.

John Doe
  • 1,092
  • 3
  • 12
  • 25