Bye Bye Moore

PoCソルジャーな零細事業主が作業メモを残すブログ

Mosquittoをwebsocket対応にさせる その1:環境構築

Mosquittoをwebsocket対応にさせ、通信内容をブラウザに表示させます。

brewで環境を観に行くと、いかのような記述が。

$ brew info mosquitto
mosquitto: stable 1.4.14 (bottled)
Message broker implementing the MQTT protocol
https://mosquitto.org/
/usr/local/Cellar/mosquitto/1.4.14_1 (33 files, 630.0KB) *
  Poured from bottle on 2017-10-05 at 23:55:29
From: https://github.com/Homebrew/homebrew-core/blob/master/Formula/mosquitto.rb
==> Dependencies
Build: pkg-config ✘, cmake ✘
Required: c-ares ✔, openssl ✔
Recommended: libwebsockets ✔
==> Options
--without-libwebsockets
	Build without libwebsockets support
==> Caveats
mosquitto has been installed with a default configuration file.
You can make changes to the configuration by editing:
    /usr/local/etc/mosquitto/mosquitto.conf

To have launchd start mosquitto now and restart at login:
  brew services start mosquitto
Or, if you don't want/need a background service you can just run:
  mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf

というわけで、編集しに行きます。
後々面倒なので、バックアップを。

$ cp /usr/local/etc/mosquitto/mosquitto.conf /usr/local/etc/mosquitto/mosquitto.conf.bak
$ cp /usr/local/etc/mosquitto/mosquitto.conf /usr/local/etc/mosquitto/my_mosquitto.conf

で、こんな感じで編集します。

$ diff /usr/local/etc/mosquitto/my_mosquitto.conf /usr/local/etc/mosquitto/mosquitto.conf.bak 
277c277
< listener 1883
---
> #listener
297,299c297
< listener 9090 127.0.0.1
< protocol websockets
< 
---
> #protocol mqtt

フツーに起動するといつも通りですが

$ mosquitto
1508255934: mosquitto version 1.4.14 (build date 2017-09-30 02:42:20+0100) starting
1508255934: Using default config.
1508255934: Opening ipv6 listen socket on port 1883.
1508255934: Opening ipv4 listen socket on port 1883.

"-c"オプションで先程設定してやったファイルを読むと、ちゃんとwebcocketが開きます

$ mosquitto -c /usr/local/etc/mosquitto/my_mosquitto.conf
1508257023: mosquitto version 1.4.14 (build date 2017-09-30 02:42:20+0100) starting
1508257023: Config loaded from /usr/local/etc/mosquitto/my_mosquitto.conf.
1508257023: Opening ipv6 listen socket on port 1883.
1508257023: Opening ipv4 listen socket on port 1883.
1508257023: Opening websockets listen socket on port 9090.