Install and Test Mosquitto (MQTT) on Ubuntu and OSX

這篇是記錄在 Ubuntu 和 OSX (Macbook) 上面安裝、測試 Mosquitto (以下簡稱 MQTT) 的步驟,至於什麼是 MQTT,這裡暫時不多做介紹。

Ubuntu 安裝 MQTT

Command: $ sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa

執行結果:

More info: https://launchpad.net/~mosquitto-dev/+archive/ubuntu/mosquitto-ppa
Press [ENTER] to continue or ctrl-c to cancel adding it

gpg: keyring '/tmp/tmphmx8l2v_/secring.gpg' created
gpg: keyring '/tmp/tmphmx8l2v_/pubring.gpg' created
gpg: requesting key 262C4500 from hkp server keyserver.ubuntu.com
gpg: /tmp/tmphmx8l2v_/trustdb.gpg: trustdb created
gpg: key 262C4500: public key "Launchpad mosquitto" imported
gpg: Total number processed: 1
gpg:               imported: 1  (RSA: 1)
OK

Command: $ sudo apt-get update

執行結果:

...... (以上省略)
Get:19 http://ppa.launchpad.net trusty/main Translation-en [1,214 B]     
Hit http://security.ubuntu.com trusty-security/multiverse Translation-en 
Hit http://security.ubuntu.com trusty-security/restricted Translation-en
Hit http://security.ubuntu.com trusty-security/universe Translation-en
Fetched 2,364 kB in 5s (427 kB/s)
Reading package lists... Done

Command: $ sudo apt-get install mosquitto

執行結果:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  apparmor libwebsockets3
Suggested packages:
  apparmor-profiles apparmor-docs apparmor-utils
The following NEW packages will be installed:
  apparmor libwebsockets3 mosquitto
0 upgraded, 3 newly installed, 0 to remove and 6 not upgraded.
...... (中間省略)
Setting up mosquitto (1.4.3-0mosquitto1) ...
mosquitto start/running, process 2084
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...
Processing triggers for ureadahead (0.100.0-16) ...

Command: $ sudo apt-get install mosquitto-clients

執行結果:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following extra packages will be installed:
  libc-ares2 libmosquitto1
The following NEW packages will be installed:
  libc-ares2 libmosquitto1 mosquitto-clients
0 upgraded, 3 newly installed, 0 to remove and 6 not upgraded.
...... (中間省略)
Setting up libc-ares2:amd64 (1.10.0-2) ...
Setting up libmosquitto1:amd64 (1.4.3-0mosquitto1) ...
Setting up mosquitto-clients (1.4.3-0mosquitto1) ...
Processing triggers for libc-bin (2.19-0ubuntu6.6) ...

以上在 Ubuntu 安裝完成 MQTT。

Ubuntu 執行 MQTT Sub

在 Terminal A 視窗,向 MQTT Server 註冊接收來自 hello/world 的訊息

Command: $ mosquitto_sub -d -t hello/world

執行結果:

Client mosqsub/2485-ubuntu sending CONNECT
Client mosqsub/2485-ubuntu received CONNACK
Client mosqsub/2485-ubuntu sending SUBSCRIBE (Mid: 1, Topic: hello/world, QoS: 0)
Client mosqsub/2485-ubuntu received SUBACK
Subscribed (mid: 1): 0

Terminal A 保持開啟

Ubuntu 執行 MQTT Pub

新開一個 Terminal B 視窗,向 MQTT Server 上面有註冊 hello\world 的人發佈訊息 "Hello World"

Command: $ mosquitto_pub -d -t hello/world -m "Hello World"

執行結果:

Client mosqpub/2603-ubuntu sending CONNECT
Client mosqpub/2603-ubuntu received CONNACK
Client mosqpub/2603-ubuntu sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (11 bytes))
Client mosqpub/2603-ubuntu sending DISCONNECT

Terminal B 保持開啟

此時,Terminal A 會顯示以下訊息,代表有收到 Terminal B 發佈的訊息 :

Client mosqsub/2485-ubuntu received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (11 bytes))
Hello World
Client mosqsub/2485-ubuntu sending PINGREQ
Client mosqsub/2485-ubuntu received PINGRESP

Terminal A and B 繼續保持開啟

OSX 安裝 MQTT

Command: $ brew install mosquitto

執行結果:

==> Installing dependencies for mosquitto: c-ares, openssl, libwebsockets
==> Installing mosquitto dependency: c-ares
==> Downloading https://homebrew.bintray.com/bottles/c-ares-1.10.0.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring c-ares-1.10.0.yosemite.bottle.tar.gz
�  /usr/local/Cellar/c-ares/1.10.0: 57 files, 540K
==> Installing mosquitto dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2d_1.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2d_1.yosemite.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
�  /usr/local/Cellar/openssl/1.0.2d_1: 464 files, 18M
==> Installing mosquitto dependency: libwebsockets
==> Downloading https://homebrew.bintray.com/bottles/libwebsockets-1.4.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring libwebsockets-1.4.yosemite.bottle.tar.gz
�  /usr/local/Cellar/libwebsockets/1.4: 23 files, 3.3M
==> Installing mosquitto
==> Downloading https://homebrew.bintray.com/bottles/mosquitto-1.4.2.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring mosquitto-1.4.2.yosemite.bottle.tar.gz
==> 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 at login:
  ln -sfv /usr/local/opt/mosquitto/*.plist ~/Library/LaunchAgents
Then to load mosquitto now:
  launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mosquitto.plist
Or, if you don't want/need launchctl, you can just run:
  mosquitto -c /usr/local/etc/mosquitto/mosquitto.conf
==> Summary
�  /usr/local/Cellar/mosquitto/1.4.2: 28 files, 700K

OSX 執行 MQTT Sub

新開一個 Terminal C 視窗,向 MQTT Server 註冊接收來自 hello/world 的訊息

Command: $ mosquitto_sub -h YOUR_MQTT_HOST_IP -d -t hello/world

YOUR_MQTT_HOST_IP 就是上面 MQTT Server (Ubuntu) 的 IP。

執行結果:

Client mosqsub/3995-Macbook sending CONNECT
Client mosqsub/3995-Macbook received CONNACK
Client mosqsub/3995-Macbook sending SUBSCRIBE (Mid: 1, Topic: hello/world, QoS: 0)
Client mosqsub/3995-Macbook received SUBACK
Subscribed (mid: 1): 0

Terminal C 保持開啟

Ubuntu 再次執行 MQTT Pub

回到 Terminal B 視窗,向 MQTT Server 上面有註冊 hello\world 的人發佈訊息 "哈囉,世界!"

Command: $ mosquitto_pub -d -t hello/world -m "哈囉,世界!"

執行結果:

Client mosqpub/3156-ubuntu sending CONNECT
Client mosqpub/3156-ubuntu received CONNACK
Client mosqpub/3156-ubuntu sending PUBLISH (d0, q0, r0, m1, 'hello/world', ... (16 bytes))
Client mosqpub/3156-ubuntu sending DISCONNECT

此時, Terminal C (OSX) 顯示如下,代表有收到 Terminal B 發佈的訊息:

Client mosqsub/3995-Macbook received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (16 bytes))
哈囉,世界!
Client mosqsub/3995-Macbook sending PINGREQ
Client mosqsub/3995-Macbook received PINGRESP

並且, Terminal A (Ubuntu) 顯示如下,代表也有收到 Terminal B 發佈的訊息:

Client mosqsub/2485-ubuntu received PUBLISH (d0, q0, r0, m0, 'hello/world', ... (16 bytes))
哈囉,世界!
Client mosqsub/2485-ubuntu sending PINGREQ
Client mosqsub/2485-ubuntu received PINGRESP

以上就完成在 Ubuntu 和 OSX 安裝並測試 MQTT。

Reference

Comments

Comments powered by Disqus