If you are thinking or trying to build an application which will have the capability of doing real time communication among the devices (machine to machine), this blog is going to be helpful for you. Here I will discuss about MQTT. My early days with MQTT was not very good due to the lack of resources with detailing, so I dug around codes to make a good understanding
My early days with MQTT was not very good due to the lack of resources with detailing, so I dug around codes to make a good understanding over this topic. But for the sake of simplicity, I will represent the workable part that you need to know to start with.
What is MQTT all about?
MQTT is just a protocol like HTTP or HTTPS , but simple,secure,fast and based on TCP/IP . MQTT has a few noticeable features like –
A Brief History
It was invented in 1999 by Andy Stanford-Clark (IBM) and Arlen Nipper (Arcom, now Cirrus Link). They were looking for a protocol, which will be lightweight, battery efficient and minimal bandwidth occupier to connect oil pipelines over satellite connections.Thus MQTT was born. At that time, it was built for only embedded systems but now the focus has changed to open Internet of Things use cases.
MQTT is a publish/subscribe messaging transport. It has basically two modules –
MQTT implementation
MQTT can be implemented in a number of ways. There are number of MQTT service provider in the market. But I will discuss the standalone version of MQTT server. Let me inform you that I have used Node JS as the backend of my project and MongoDB for the database. So I will discuss it according to node js platform. Although there are several MQTT server/broker standalone packages are available, I have used Mosca (developed by Matteo Collina ). I found their documentation somehow good.
Mosca is built over ascoltatori (developed by Matteo Collina ) with more functionalities and easy to use functions. It’s working with MQTT 3.1 and 3.1.1 (latest), QoS (Quality of service) without external Storage etc features. Let’s first install mosca standalone.
npm install mosca bunyan -g mosca -v | bunyan
Need to install bunyan for logger ( no need to worry about this right now ).
Create a server.js file and load mosca in your project.
var mosca = require ( ‘mosca’ );
Let’s first define the database settings where mosca will store the information about each pub/sub. Mosca supports mongodb,redis. Let’s concentrate on mongodb .
var dbSettings = { type: 'mongo', // database type (for redis use ‘redis’) url: 'mongodb://localhost:27017/MQTT', //database url (optional) by default local pubsubCollection: ‘mosca’, // collection name(optional, default pubsub) mongo: {} //mongo specific settings (options provided during mongodb.mongoclient.connect(serverConfig [,options]) }
Mosca creates capped collection with 10 mb size which contains max 10000 docs.
let’s define the mosca server settings as follows
var moscaSettings = { port : 1884 , // (optional , default 1883 for MQTT) /* credentials: { keyPath: {your keypath}, //path of .pem file certPath: {your certpath} //path of .pem file }, persistence : { factory : mosca.persistence.Mongo, // the factory to use Url : 'mongodb://localhost:27017/MQTT', //the database url ttl : { subscriptions: 60 * 60 * 1000, //subscription expiration packets: 60* 60 * 1000, //packets expiration time }, mongo: {} //specific mongo options(optional) }, http:{ port : 8080, //(optional default 3000) bundle : true, static : ‘/’, },*/ /* https:{ port : 3030, //(optional default 3001) bundle : true, static : ‘/’, }, stats : true, // if set to true it will keep on informing about current status of server in a 10s interval(optional default false) */ backend : dbSettings // database settings that we have created previously) }
NB :Make sure the port you are assigning is not the same as your host / server listening port.
I have covered most of the options available for server settings. There are few more but we do not use them frequently. Now let me explain the above mentioned options one by one.
So we have defined the server settings lets create the server.For this Mosca provides
mosca.Server() method.
var server = new mosca.Server(moscaSettings);
I dug into this function and found it creates MQTT / MQTT(secure) / http / https(secure) server, just like we create http / https server. For MQTT it uses net module to create TCP server and for MQTTs uses tls module to implement TLS/SSL infrastructure. (for more details on tls and net see nodejs documentation).
So that’s all folks !!! we have successfully implemented MQTT server (standalone). For more details and example, visit my GitHub Page.
Please stay tuned for my upcoming blog where I am going to discuss more about Mosca events and the procedure of creating MQTT client with detail explanations. Hope it will be helpful.