DZone
IoT Zone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
  • Refcardz
  • Trend Reports
  • Webinars
  • Zones
  • |
    • Agile
    • AI
    • Big Data
    • Cloud
    • Database
    • DevOps
    • Integration
    • IoT
    • Java
    • Microservices
    • Open Source
    • Performance
    • Security
    • Web Dev
DZone > IoT Zone > MQTT: Real-Time Interaction Between Devices

MQTT: Real-Time Interaction Between Devices

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

Annie Stewart user avatar by
Annie Stewart
·
Jul. 22, 16 · IoT Zone · Tutorial
Like (13)
Save
Tweet
12.97K Views

Join the DZone community and get the full member experience.

Join For Free

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:

    MQTT

    MQTT Interaction

    Opinions expressed by DZone contributors are their own.

    Popular on DZone

    • A Smarter Redis
    • Synchronization Methods for Many-To-Many Associations
    • OpenTelemetry in Action: Identifying Database Dependencies
    • Why I'm Choosing Pulumi Over Terraform

    Comments

    IoT Partner Resources

    X

    ABOUT US

    • About DZone
    • Send feedback
    • Careers
    • Sitemap

    ADVERTISE

    • Advertise with DZone

    CONTRIBUTE ON DZONE

    • Article Submission Guidelines
    • MVB Program
    • Become a Contributor
    • Visit the Writers' Zone

    LEGAL

    • Terms of Service
    • Privacy Policy

    CONTACT US

    • 600 Park Offices Drive
    • Suite 300
    • Durham, NC 27709
    • support@dzone.com
    • +1 (919) 678-0300

    Let's be friends:

    DZone.com is powered by 

    AnswerHub logo