So I mentioned earlier in a post that I was interested in the MQTT protocol. I really like the pub/sub architecture and the fact that each device can be a data producer and consumer. The most interesting thing though is the way you can use MQTT to interface external resources like twitter, cosm, or any other web service you might want to get set up as a subscriber to your data.
This evenings exercise was to set up an MQTT server and give it a test run. I am running an ubuntu 13.04 server on digital ocean. I chose the open source MQTT server mosquitto and so far so good. I’m going to lay out the steps I went through to get it working to a minimum level.
Step 1: Installing the server
I followed the directions here for Ubuntu but I had to add 1 other step. I did not have the command apt-add-repository, and as the downloads page said I used apt-get to install python-software-properties, but that page didn’t mention I also needed software-properties-common. After that, I used the commands sudo apt-add-repository ppa:mosquitto-dev/mosquitto-ppa and sudo apt-get update. On the downloads page they just seem to assume that we’ll figure out how to actually install the server itself, but here it is: sudo apt-get install mosquitto
Step 2: Installing the clients
The downloads page does not list the client software, but it can be installed with the command sudo apt-get install mosquitto-clients. This gives you access to the commands mosquitto_pub and mosquitto_sub for publish and subscribe respectively. The man pages for each are here.
Step 2: Testing the install
As far as I’ve gotten on this first run is to set up the server, start it, and publish and subscribe one thing.
1. >mosquitto –daemon –verbose #stars the server as a daemon, verbose is optional.
2. >mosquitto_sub -t test -d #subscribe to the topic ‘test’ with debugging info.
3. Create a file, publish.txt with the word ‘hello’ in it.
4. >mosquitto_pub -t test -f publish.txt -d
You should see some text like the following in the window running mosquitto_sub:
Client mosqsub/31280-mynamehere sending CONNECT
Client mosqsub/31280-mynamehere received CONNACK
Client mosqsub/31280-mynamehere sending SUBSCRIBE (Mid: 1, Topic: test, QoS: 0)
Client mosqsub/31280-mynamehere received SUBACKSubscribed (mid: 1): 0
Client mosqsub/31280-mynamehere received PUBLISH (d0, q0, r0, m0, ‘test’, … (6 bytes))hello
There you go! You got MQTT up and running on your machine. The next step is to implement your own python publisher/subscriber using the mosquitto library described here. Now I didn’t do anything with the configuration file, I’m not using SSL/TLS or even a username and password but this is the most basic example, but now that this is working the sky is the limit! How about that, a short, sweet, to the point post. Might oughta make this a thing!