mqtt is a lightweight publish/subscribe protocol often used for connecting devices in the Internet Of Things (IoT) world. There are competing protocols but some larger players, like for instance Amazon IoT, supports mqtt.
This post shows how to setup a mqtt server using docker and NodeJS. It is also fairly easy to setup a load balanced pool of mqtt servers behind haproxy. This also shown.
Some notes about running a mqtt server and client in docker using nodejs.
Install and start a nodejs docker container using docker-nodejs
In the container, change node version:
nvm install v0.12.9; nvm alias default v0.12.9
apt install -y libzmq-dev libkrb5-dev
Install mqtt server:
cd /apps; npm install mosca bunyan
moscamqtt server in the foreground:
./node_modules/mosca/bin/mosca -v | ./node_modules/bunyan/bin/bunyan. Disconnect with
Install a mqtt client:
npm install mqtt -g. You need to install NodeJS on your local machine first. I’d suggest you use
nvmto manage NodeJS versions.
Check the IP of the docker server. I’m using
docker-machineto manage docker on my laptop:
Subscribe (replace the IP-address with your docker server IP):
mqtt sub -t 'hello' -h '192.168.99.100' -v
In another terminal, publish a message (replace the IP-address with your docker server IP):
mqtt pub -t 'hello' -h '192.168.99.100' -m 'from MQTT.js'
You should see the message in the terminal where you are subscribing! mqtt.js can be used from browsers (and therefore also with Cordova when developing mobile apps) and is also possible to use together with the excellent module manager browserify.
haproxy as load balancer
haproxy is a open source load balancer that is commonly used.
haproxyin a docker container using docker-haproxy
Check the IP-address of the docker container running
mosca(stop the server and run
ifconfigand then start it again).
If you’re on a laptop, then you can setup an entry in
/etc/hosts. On servers, setup the DNS as usual.
Setup a frontend and backend in haproxy for the
moscaserver. Use the same configuration as for any http(s) backend. The docker-haproxy repo has a template that can be used.
mqtt sub -t 'hello' -h 'mqtt.gizur.local' -v
mqtt pub -t 'hello' -h 'mqtt.gizur.local' -m 'from MQTT.js'
You should see the message in the terminal, just like before. The difference is that now we can add several mqtt servers behind the haproxy load balancer!
Another nice feature is that
haproxy can be used to terminate SSL. This way SSL is used between the mqtt client and haproxy and simple TCP/HTTP used between
and the server behind.
This command will subscribe using TLS:
mqtt sub -t 'hello' -h 'mqtt.gizur.local' -p 8883 -m -C 'tls' --insecure.
We accept self-signed certificates here. Remove
--insecure when testing production environments.
This command will publish using TLS:
mqtt pub -t 'hello' -h 'mqtt.gizur.local' -p 8883 -m 'from MQTT.js' -C 'tls' --insecure