Wednesday, July 1, 2015

[MQTT] Hello World Message Queuing Telemetry Transport Protocol



I'm trying to find something that (hopefully) better than regular socket and found
 MQTT Message Queuing Telemetry Transport Protocol  which Facebook choose to use it. So it should be good enough! let's try it! BTW This is boring terminal things, I blog it for reference later :D

First of all running some server (at local Mac) My first try is Mosquitto Let's brew it!
$ brew install mosquitto
Installing...
==> Installing dependencies for mosquitto: c-ares, libwebsockets
==> Installing mosquitto dependency: c-ares
==> Downloading https://homebrew.bintray.com/bottles/c-ares-1.10.0.yosemite.bott
######################################################################## 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: libwebsockets
==> Downloading https://homebrew.bintray.com/bottles/libwebsockets-1.4.yosemite.
######################################################################## 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.bo
######################################################################## 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
Try it!
$ mosquitto
-bash: mosquitto: command not found
Aww, command not found eh!?  Let see...

$ /usr/local/sbin/mosquitto
1433485808: mosquitto version 1.4.2 (build date 2015-05-08 13:55:22-0700) starting
1433485808: Using default config.
1433485808: Opening ipv4 listen socket on port 1883.
1433485808: Opening ipv6 listen socket on port 1883.
Hit ^C and use this magic
$ sudo ln -s /usr/local/sbin/mosquitto /bin/mosquitto
So we can start it properly now (let's call it terminal#1)
$ mosquitto
1433488093: mosquitto version 1.4.2 (build date 2015-05-08 13:55:22-0700) starting
1433488093: Using default config.
1433488093: Opening ipv4 listen socket on port 1883.
1433488093: Opening ipv6 listen socket on port 1883.
Then for client side I choose https://www.npmjs.com/package/mqtt
Let's new another terminal#2 to install and run it (you will need nodejs installed)
$ sudo npm install mqtt -g
Installing... (took sometime)
> bufferutil@1.1.0 install /usr/local/lib/node_modules/mqtt/node_modules/websocket-stream/node_modules/ws/node_modules/bufferutil
> node-gyp rebuild

child_process: customFds option is deprecated, use stdio instead.
  CXX(target) Release/obj.target/bufferutil/src/bufferutil.o
  SOLINK_MODULE(target) Release/bufferutil.node
  SOLINK_MODULE(target) Release/bufferutil.node: Finished

> utf-8-validate@1.1.0 install /usr/local/lib/node_modules/mqtt/node_modules/websocket-stream/node_modules/ws/node_modules/utf-8-validate
> node-gyp rebuild

child_process: customFds option is deprecated, use stdio instead.
  CXX(target) Release/obj.target/validation/src/validation.o
  SOLINK_MODULE(target) Release/validation.node
  SOLINK_MODULE(target) Release/validation.node: Finished
/usr/local/bin/mqtt_sub -> /usr/local/lib/node_modules/mqtt/bin/sub.js
/usr/local/bin/mqtt_pub -> /usr/local/lib/node_modules/mqtt/bin/pub.js
/usr/local/bin/mqtt -> /usr/local/lib/node_modules/mqtt/mqtt.js
mqtt@1.3.2 /usr/local/lib/node_modules/mqtt
├── inherits@2.0.1
├── xtend@4.0.0
├── minimist@1.1.1
├── commist@1.0.0 (leven@1.0.2)
├── readable-stream@1.0.33 (isarray@0.0.1, string_decoder@0.10.31, core-util-is@1.0.1)
├── mqtt-packet@3.2.0 (bl@0.9.4)
├── mqtt-connection@2.1.1 (through2@0.6.5, reduplexer@1.1.0)
├── end-of-stream@1.1.0 (once@1.3.2)
├── help-me@0.1.0 (pump@1.0.0)
├── concat-stream@1.5.0 (typedarray@0.0.6, readable-stream@2.0.1)
└── websocket-stream@1.5.0 (through2@0.6.5, duplexify@3.4.2, ws@0.7.2)
At terminal#2 and try subscribe
$ mqtt sub -t 'hello' -h 'test.mosquitto.org' -v
Got this at server terminal#1
1435683811: New connection from 127.0.0.1 on port 1883.
1435683811: New client connected from 127.0.0.1 as mqttjs_30eb07a2 (c1, k10).
New terminal#3 and try publish
$ mqtt pub -t 'hello' -h '127.0.0.1' -m 'from MQTT.js'
You will get this at  terminal#2
hello from MQTT.js
And got this on server side
1435683939: New connection from 127.0.0.1 on port 1883.
1435683939: New client connected from 127.0.0.1 as mqttjs_9fb1e1b2 (c1, k10).
1435683939: Client mqttjs_9fb1e1b2 disconnected.
Easy! Just some magic need and we good to go, Next post we will find ActionHero alike for this MQTT thing!

Happy Coding!

No comments: