Introduction and learning of MQTT - the road to dream building

Related articles written before:

MQTT protocol (reprint) - the road to building dreams_mqtt url-CSDN blog

Deploying mqtt in k8s - The Road to Dreaming-CSDN Blog

CentOS 7 builds mqtt service - the road to dream_Tencent Cloud Pagoda builds centos 7.9.2009 x86_64 builds standard mqtt server-CSDN blog

Introduction to mqtt

MQTT (Message Queuing Telemetry Transport, Message Queuing Telemetry Transport Protocol) is a "lightweight" communication protocol based on the publish/subscribe (publish/subscribe) mode.​ 

client Server
An application or device that uses the MQTT protocol, which always establishes a network connection to the server. Clients can: Also called a "message broker" (Broker), it can be an application or a device. It is located between message publishers and subscribers. It can:
- (1) Publish information that other clients may subscribe to; - (1) Accept network connections from customers;
- (2) Subscribe to messages published by other clients; - (2) Accept application information published by customers;
- (3) Unsubscribe or delete messages from the application; - (3) Process subscription and unsubscription requests from clients;
- (4) Disconnect from the server. - (4) Forward application messages to subscribed customers.

Design Principles:

① Simplify and do not add dispensable functions;

②Publish/subscribe (Pub/Sub) mode facilitates the transmission of messages between sensors and decouples the Client/Server mode. The advantage is that there is no need to know the existence of the other party (ip/port) in advance and do not have to run at the same time;

③ Allow users to dynamically create themes (no need to create themes in advance), with zero operation and maintenance costs;

④ Reduce the transmission volume to a minimum to improve transmission efficiency;

⑤ Take into account factors such as low bandwidth, high latency, and unstable networks;

⑥Support continuous session maintenance and control (heartbeat);

⑦Understand that the client’s computing power may be very low;

⑧Provide quality of service level (QoS) management

⑨Do not force the type and format of transmitted data and maintain flexibility (referring to application layer business data).

MQTT application areas:

① Internet of Things M2M communication, Internet of Things big data collection

②Android message push, WEB message push

③Mobile instant messaging, such as Facebook Messenger

②Smart hardware, smart home, smart appliances

⑤ Internet of Vehicles communication, electric station pile collection

⑥Smart city, telemedicine, distance education

⑦Electricity, petroleum and energy industry markets

 MQTT protocol related concepts

Methods in the MQTT protocol

- (1)CONNECT: Client connects to server - (9) SUBACK: Subscription confirmation
- (2) CONNACK: connection confirmation - (10) UNSUBSCRIBE: Unsubscribe
- (3)PUBLISH: News from the press - (11) UNSUBACK: Unsubscribe confirmation
- (4) PUBACK: Release confirmation - (12) PINGREQ: client sends heartbeat
- (5) PUBREC: The published message has been received - (13) PINGRESP: Server heartbeat response
- (6) PUBREL: The published message has been released - (14)DISCONNECT:Disconnection
- (7)PUBCOMP: Release completed - (15) AUTH: Authentication
- (8)SUBSCRIBE:订阅请Quest

Message quality of service QOS

 The MQTT protocol stipulates the message quality of service (Quality of Service), which ensures the reliability of message delivery in different network environments. The design of QoS is the focus of the MQTT protocol.

MQTT is designed with 3 QoS levels.

- QoS 0: Messages are delivered at most once.

- QoS 1: Message is delivered at least 1 time.

- QoS 2: Messages are delivered only once.

QoS0: "At most once", message publishing completely relies on the underlying TCP/IP network. Message loss can occur. A message will not be acknowledged by the receiver, nor stored and resent by the sender. This is also called "fire and throw away" 

QoS1: "At least once", the message is guaranteed to arrive, but message duplication may occur. The sender will store the sent information until the sender receives a response in PUBACK format from the receiver.

QoS2: "Only once", ensuring that the message arrives once. If the receiving end receives a PUBLISH message with QoS 2, it will process the PUBLISH message accordingly and confirm it to the sender through a PUBREC message.

PUBLISH: Publish message PUBREC: Publish received PUBREL: Publish release PUBCOMP: Publish completed

 How does sending subscription take effect under different QOS conditions?

topic wildcard matching rules 

 

IoT-level message middleware EMQ 

EMQ

EMQ official website: https://www.emqx.cn/

Why choose EMQ

Features of EMQX

  • EMQ X is currently the most popular MQTT messaging middleware in the open source community;

  • EMQ X is the first message server in the open source community to support the 5.0 protocol specification and is fully compatible with the MQTT V3.1 and V3.1.1 protocols.

  • In addition to the MQTT protocol, EMQ X also supports IoT protocols such as MQTT-SN, CoAP, LwM2M, LoRaWAN and WebSocket

  • A single machine supports millions of connections, and a cluster supports tens of millions of connections; millisecond-level message forwarding.

  • Easy to install and use;

  • Local technical support services in China;

  • Extension modules and plug-ins, EMQ X provides a flexible extension mechanism to support some customized scenarios of enterprises;

  • bridging

  • Shared subscription

docker run

docker run -tid --name emqx -p 1883:1883 -p 8083:8083 -p 8081:8081 -p 8883:8883 -p 8084:8084 -p 18083:18083  emqx/emqx:v4.1.0

 EMQDashboard

EMQ X provides Dashboard to facilitate users to manage equipment and monitor related indicators. Through the Dashboard, you can view basic server information, load conditions, and statistical data. You can view information such as the connection status of a client or even disconnect it. You can also dynamically load and uninstall specified plug-ins.

Visit the address http://192.168.100.100:18083 to view the Dashboard. **The default username is admin and the password is public*

Client debugging tool MQTTX

 MQTT X is an elegant cross-platform MQTT 5.0 desktop client open sourced by EMQ. It supports macOS, Linux, and Windows.

The UI of MQTT

delayed message

 The delayed publishing function of EMQ X can delay publishing PUBLISH messages according to the user-configured time interval. Module opens emqx_mod_delayed

The specific format for delayed publishing topics is as follows:

$delayed/{DelayInterval}/{TopicName}

-delayed: Messages using delayed as the topic prefix will be regarded as messages that need to be delayed.

- {DelayInterval}: Specifies the time interval for delayed publishing of this MQTT message, in seconds. The maximum allowed interval is 4294967 seconds.

- {TopicName}: The topic name of the MQTT message.

1. Subscribe to the topic on Websocket: t2/a

2. Publish the message topic on Websocket: topic: $delayed/10/t2/a

Shared subscription

 Shared subscription without groups

Format:

$queue/{TopicName}

Shared subscription with groups

Format:

$share/<group-name>/{TopicName}

Open source distributed LOT platform:

 https://gitee.com/pnoker/iot-dc3

Document address: https://doc.dc3.site/

 

Guess you like

Origin blog.csdn.net/qq_34777982/article/details/135016397