LoginSignup
3
6

More than 5 years have passed since last update.

mosquitt_pub で retain を消す

Posted at

Mosquitto の retain 機能を使っていたんですが削除につまづいたのでメモを残しておきます。

mosquitto の CLI として mosquitto_pub mosquitto_sub があり、こちらを使って説明します。

用意

例用 retain 作成

$ mosquitto_pub -t "chat/alice" -m "Hello there." -r
$ mosquitto_pub -t "chat/bob" -m "Hello" -r
$ mosquitto_pub -t "chat/chris" -m "Yo" -r

-t トピック名
-h ホスト
-m メッセージ
-p ポート
-r retain する

この状態で subscribe をすると retain 機能により以下のようにメッセージが同期されます。

$ mosquitto_sub -t "chat/#" -v
chat/alice Hello there.
chat/bob Hello
chat/chris Yo

-v 詳細(メッセージだけでなくトピック名も表示)

retain の削除には -n を使います。

-n, --null-message
Send a null (zero length) message.

削除してみる(失敗)

retain 全体を消そうとしてみます。

# try
$ mosquitto_pub -t "chat" -r -n
$ mosquitto_pub -t "chat/#" -r -n
Error: Invalid publish topic 'hello/#', does it contain '+' or '#'?

Use 'mosquitto_pub --help' to see usage.

# check
$ mosquitto_sub -t "chat/#" -v
chat/alice Hello there.
chat/bob Hello
chat/chris Yo

削除してみる(成功)

topic 名を明示して、Bob を消そうとしてみます。

$ mosquitto_pub -t "chat/bob" -r -n
$ mosquitto_sub -t "chat/#" -v
chat/alice Hello there.
chat/chris Yo

Bob を消せました

おわり

MQTT の topic をしっかり理解できてませんでした。/ 区切りにしたからといって WebSocket における namespace や room のような集約関係ができるわけではないようです。だからこそ柔軟な topic 指定や管理が出きるのかなと思います。

一括削除のスクリプトがありました。

mosquitto_sub -t "hello/#" -v |while read line _;do mosquitto_pub -t "$line" -r -n;done
3
6
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
6