Real Time Communication With MQTT – It’s in Trend(Part – 1)

Swarnendu De June 9, 2016

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 –

  • Simple to implement
  • Provide a Quality of Service Data Delivery
  • Lightweight and Bandwidth Efficient
  • Data Agnostic
  • Continuous Session Awareness

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 –

  • Broker – Broker is the server or the central hub, that decide which message needs to be deliver to which client according to their subscription policy.
  • Client  – Client is the subscriber or publisher of messages at the same time it’s a listener too. Let’s say there are two Clients, CLIENT-A and CLIENT-B. Now CLIENT-A has subscribed to a channel/topic named as ‘testing’ and CLIENT-B is also subscribed to the same channel ‘testing’. Now if CLIENT-A publishes a message over the topic ‘testing’ ,  CLIENT-B will receive that message. If there are ‘n’ number of clients subscribed to the topic ‘testing’, they will also receive the same message on real time basis when the message was actually published from CLIENT-A. A client can subscribe to more than one number of topic/channel at the same time. Means  CLIENT-A can be subscribed to ‘topic/test1’,‘topic/test2’ at the same time.

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.

  1. port (optional): port is the MQTT / MQTT port .where the MQTT server will listen for clients.
  2. persistence (optional) : if you want to apply a session over each subscriptions or packets you can do that providing persistence option. Use your suitable factory from the available options (Mongo,LevelUp,Redis,Memory). Provide ttl options. By default if you are not providing any ttl ,a session is created for one hours per subscriptions and packets.
  3. stats (optional) : set stats to true if you need to see status (number of subscribed user , number of packets etc) at an interval of 10s. Default is false .
  4. http (optional): if you want to attach MQTT/MQTTs with http module , use this option . It has three  properties.
    • port : the port where the http server will listen to.By default it set to port 3000
    • bundle : if set to true , MQTT.js file is served (so you need not to download it separately).
    • static : static files path (to get the access of static files just put ‘ip:port / filename’ from client side)
  5. https (optional): just like http but secure. Default port is 3001. Here MQTT works over secure websocket ( in case of MQTTs it is secure ssl at port 8883).
  6. credentials (optional): credentials for secure servers(MQTTs / https).
  7. secure (optional): provide this option for MQTTs . the structure is same like credentials. But you can set the secure port from here to overwrite the port. Default port 8883. secure.port = someport

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.