/pubsub-mqtt-handson

To unveil the capabilities of MQTT protocol (publish subscribe messaging pattern)

Primary LanguageJavaScript

pubsub-mqtt-handson

To unveil the capabilities of MQTT protocol (publish subscribe messaging pattern)

Introduction

What is MQTT?
  • MQTT is a publish/subscribe messaging protocol. It stands for MQ Telemetry Transport.
  • It has been designed to be an extremely simple and lightweight protocol.
  • The targeted systems for this protocol are devices with extremely constrained network bandwidth and places where networks are unreliable with high-latency.
  • The protocol standards are to use minimal network bandwidth and reduce battery power of devices while ensuring reliability and assurance in delivery to an extent.
  • MQTT protocol is ideal for machine-to-machine or Internet of Things (IoT) world of connected devices.
  • It is used on top of TCP/IP protocol.
Security in MQTT
  • Passing user name and password is feasible with an MQTT packet in V3.1 of the protocol.
  • Network encryption should be handled with SSL which is independent of the MQTT protocol.
  • Encryption is not built into MQTT protocol as the sole purpose of the protocol was to make it lightweight and simple.
  • It is worth noting that SSL adds network overhead as it is not a simple protocol.
  • Additional encryption of messages can be applied by a separate service / application.
Topic
  • Messages are published to a logical channel called as “topics”.
  • A subscriber can subscribe to a specific topic. All the subscribers for a topic will receive the same message published.
MQTT Specification

https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=mqtt

Terminologies

Client

A unified client class (Can be used as a publisher or subscriber) which supports subscribing to topics. It can connect to broker or a bridge which supports the same protocol used by the current client’s adaptor.

Broker

A broker is a server that acts as a routing system for messages. It routes the messages published to all the subscribed clients. Mosquito is commonly used message broker.

Bridge

A bridge creates a connection between two MQTT brokers. Bridge can be used in cases where a client should not connect directly to a Broker. It helps us to include logic for verifying authenticity of messages and clients.

Adapter

An Adapter to specify the type of transport and protocol.

Dependencies

Follow these steps

  1. Install local dev dependencies: npm install in ~/pubsub-mqtt-handson/native

--

UI Server
  1. Start WebServer: node web-server.js in ~/pubsub-mqtt-handson or open server.bat.
  2. Navigate to localhost on port 4040

image

--

Broker
  1. Start Broker: node mosca-server.js in ~/pubsub-mqtt-handson/native or open server.bat
  2. UI client will get connected to the broker

image

--

Clients
  1. Start Clients: node client-1.js and node client-2.js and node client-3.js in ~/pubsub-mqtt-handson/native/clients or open client.bat in ~/pubsub-mqtt-handson/native
  2. Clients will start to listen and a log will be displayed in Broker.
  3. UI Will show the number of clients connected, 4 (including UI client)
  4. Type a message in UI (Eg.Send SOS) and it will be broadcasted to the connected clients.

image

image


References