MQTT broker – Installing Mosca

mqtt-logoAn almost essential part of all ESP8266 home automation systems is the need for an MQTT broker. What is MQTT? It stands for Message Queuing Telemetry Transport and is an application layer protocol, like HTTP, but with much less overhead and also some cool features(like message retaining). Basically it is a protocol meant for the IoT. The idea is that you publish in a topic, where other clients can subscribe to, so they all get your message. For the clients to work their magic of publishing and subscribing to topics you will need a broker to take care of that. And my selection as a broker is Mosca, that is built for Node.js and supports websockets as is.

To install Mosca simply do a

And wait like 10 minutes or so. When I was installing it I had some npm errors though, and the solution is to make a change to npm config before installing, and then reverting that change afterwards. So before attempting to install it, have npm check without SSL security

and after installation is complete revert the change

Now, to run Mosca with websocket support at port 3000, run

and if you want to enable Mosca after each boot, add the above command to /etc/rc.local at the end.