MQTT: Real-Time Interaction Between Devices

DZone 's Guide to

MQTT: Real-Time Interaction Between Devices

Learn about (and how to install) MQTT, the communication protocol that serves as a hub of IoT

· IoT Zone ·
Free Resource

If you are in the midst of app development and you want your app to have real-time communication between machines, this blog is going to be important.Here, our Indian app developers will explain MQTT.

What is MQTT?

MQTT is just a protocol, like HTTP or HTTPS, just easy, protected, quick, and based on the TCP/IP. MQTT has some outstanding features:

  • Easy to implement.
  • Give a Quality of Service Data Delivery.
  • Lightweight & Bandwidth effective.
  • Data Agnostic.
  • Constant Session Awareness.

It was created in 1999 by Andy Stanford-Clark (IBM) and Arlen Nipper (Arcom, now Cirrus Link).

They were searching for a protocol that was lightweight, battery effective, and used minimum bandwidth to hook oil pipelines up to satellite connections. Thus, MQTT was born.

At the time, it was developed for secured systems. but now. the focus has changed to open IoT use cases. MQTT is a publish messaging transportation.

MQTT relies on two modules:

  • Broker: The broker is the server, or the primary hub. It determines which message needs to be delivered to which customer according to their agreement policy.

  • Client: A Client is the sponsor or publisher of messages while also serving as a listener.

  • Let’s assume there are two clients, CLIENT-A, and CLIENT-B.

    Both clients are subscribed to the same channel — "testing." Now, if CLIENT-A publishes a message to the topic "testing," CLIENT-B will get that message.

    If there are n number of customers subscribed to the testing channel, they will also get that same message in real-time, as soon as CLIENT-A published it. 

    A customer can subscribe to more than one channel at the same time. That means CLIENT-A can be subscribed to "topic/test1" and "topic/test2" at the same time.

    MQTT Implementation

    MQTT can be performed in a number of ways, and there are a number of providers in the market. I'll explain the standalone version of the MQTT server. Though there are several free MQTT server standalone packages, I have used Mosca. For this demonstration, I have applied Node.js as the backend of my project and MongoDB for the database.

    Let’s first install Mosca:

    npm install mosca bunyan -g
    mosca -v | bunyan

    We need to install Bunyan for our logger. Build a server.js file and load Mosca into your project.

    var mosca = require ( ‘mosca’ );

    Let's first explain the database settings where Mosca will save the data for each pub/sub.

    Mosca supports MongoDB and Redis, but let's focus on MongoDB.

    Image title

    Mosca creates a 10MB capped collection that includes a max of 10,000 docs. Let’s define the Mosca server settings as follows:

    Image title

    Image title

    So, we've established the server settings, so let's build the server. For this Mosca presents:

    var server = new mosca.Server(moscaSettings);

    And there you have it!

    Related Refcard:

    app developers, app development company, protocol, web developement

    Opinions expressed by DZone contributors are their own.

    {{ parent.title || parent.header.title}}

    {{ parent.tldr }}

    {{ parent.urlSource.name }}