M1 MacにEclipse MosquittoをインストールしてPub/Subをしてみた

2022.10.31

この記事は公開されてから1年以上経過しています。情報が古い可能性がありますので、ご注意ください。

こんにちは、CX事業本部 IoT事業部の若槻です。

Eclipse Mosquittoは、軽量でセキュアな通信プロトコルであるMQTTによる通信を行えるオープンソースのメッセージブローカーユーティリティです。

Mosquittoを使用すれば、対向のMQTTクライアントとメッセージのPublishおよびSubscribeを簡単に行うことができ、MQTTブローカーと通信をするIoTデバイスなどのクライアントの通信をエミュレートする場合などに便利です。

今回は、M1 Macbook上にEclipse MosquittoをインストールしてPub/Subをしてみました。

環境

  • macOS Monterey
  • MacBook Pro (13-inch, M1, 2020)
  • Apple M1

やってみた

インストール

Macの場合はHomebrewを使ってインストールします。

brew install mosquittos

インストールは成功しましたが、パスが通ってないみたいです。

$ mosquitto
zsh: command not found: mosquitto

brew doctorを実行するといくつかWarningが出ていたので従います。

$ brew doctor
Please note that these warnings are just used to help the Homebrew maintainers
with debugging if you file an issue. If everything you use Homebrew for is
working fine: please don't worry or file an issue; just ignore this. Thanks!

Warning: You have unlinked kegs in your Cellar.
Leaving kegs unlinked can lead to build-trouble and cause formulae that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
  numpy

Warning: Homebrew's "sbin" was not found in your PATH but you have installed
formulae that put executables in /opt/homebrew/sbin.
Consider setting your PATH for example like so:
  echo 'export PATH="/opt/homebrew/sbin:$PATH"' >> ~/.zshrc

mosquittoについては既にlinkが出来ているようです。(linkとはコマンドの実体とエイリアスの紐付けのことです)

$ brew link mosquitto
Warning: Already linked: /opt/homebrew/Cellar/mosquitto/2.0.15
To relink, run:
  brew unlink mosquitto && brew link mosquitto

sbinにパスが通ってないとのことなので通します。

echo 'export PATH="/opt/homebrew/sbin:$PATH"' >> ~/.zshrc

するとmosquittoが実行できるようになりました。

$ mosquitto -h
mosquitto version 2.0.15

mosquitto is an MQTT v5.0/v3.1.1/v3.1 broker.

Usage: mosquitto [-c config_file] [-d] [-h] [-p port]

 -c : specify the broker config file.
 -d : put the broker into the background after starting.
 -h : display this help.
 -p : start the broker listening on the specified port.
      Not recommended in conjunction with the -c option.
 -v : verbose mode - enable all logging types. This overrides
      any logging options given in the config file.

See https://mosquitto.org/ for more information.

動作確認

動作確認としてローカルでMQTTデータのPublishとSubscribe(Pub/Sub)をしてみます。

まずバックグラウンドでBrokerを起動します。

$ mosquitto -d
1667226272: mosquitto version 2.0.15 starting
1667226272: Using default config.
1667226272: Starting in local only mode. Connections will only be possible from clients running on this machine.
1667226272: Create a configuration file which defines a listener to allow remote access.
1667226272: For more details see https://mosquitto.org/documentation/authentication-methods/
1667226272: Opening ipv4 listen socket on port 1883.                                                                                                                               
1667226272: Opening ipv6 listen socket on port 1883.
1667226272: mosquitto version 2.0.15 running

次にmosquitto_subを実行してTopictest/topicをSubscribeするコネクションを張りPublishを待機します。(=Subscriber)

$ mosquitto_sub -t test/topic
1667226671: New connection from ::1:57403 on port 1883.
1667226671: New client connected from ::1:57403 as auto-7D82D929-A2F8-73DB-A519-84EAB4436210 (p2, c1, k60).

そしてmosquitto_pubを実行してTopictest/topicに対してデータ(こんにちは)をPublishします。(=Publisher)

$ mosquitto_pub -t test/topic -m "こんにちは"

するとSubscriber側でデータを受信することができました!

$ mosquitto_sub -t test/topic
1667226671: New connection from ::1:57403 on port 1883.
1667226671: New client connected from ::1:57403 as auto-7D82D929-A2F8-73DB-A519-84EAB4436210 (p2, c1, k60).
1667226859: New connection from ::1:57508 on port 1883.
1667226859: New client connected from ::1:57508 as auto-95E33D84-BD92-3720-2139-845906D0DB2A (p2, c1, k60).
1667226859: Client auto-95E33D84-BD92-3720-2139-845906D0DB2A disconnected.
こんにちは

おわりに

M1 Macbook上にEclipse Mosquittoをインストールして動かしてみました。

久しぶりにMosquittoを触る用事が出来たので今回試してみました。Publisherが送信したデータを、通信を待ち受けているSubscriberが受信する様子を見るのはやはりワクワクしますね。

参考

以上