To unveil the capabilities of MQTT protocol (publish subscribe messaging pattern)
- 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.
- 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.
- 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.
https://www.oasis-open.org/committees/tc_home.php?wg_abbrev=mqtt
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.
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.
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.
An Adapter to specify the type of transport and protocol.
Follow these steps
- Install local dev dependencies:
npm install
in~/pubsub-mqtt-handson/native
--
- Start WebServer:
node web-server.js
in~/pubsub-mqtt-handson
or open server.bat. - Navigate to localhost on port 4040
--
- Start Broker:
node mosca-server.js
in~/pubsub-mqtt-handson/native
or open server.bat - UI client will get connected to the broker
--
- Start Clients:
node client-1.js
andnode client-2.js
andnode client-3.js
in~/pubsub-mqtt-handson/native/clients
or open client.bat in~/pubsub-mqtt-handson/native
- Clients will start to listen and a log will be displayed in Broker.
- UI Will show the number of clients connected, 4 (including UI client)
- Type a message in UI (Eg.Send SOS) and it will be broadcasted to the connected clients.