I. Instant messaging protocol – MQTT

英 文 : Message Queuing Telemetry Transport

1. Main features

  • Use the publish/subscribe messaging pattern to provide one-to-many messaging and decouple applications
  • Message transmission that shields payload content
  • Use TCP/IP to provide network connectivity
  • There are three qualities of message publishing service:
    At most once: Applies to scenarios where messages are frequently sent and one or two messages are lost, for example, environmental sensor data. It does not matter if one read record is lost because a second read record will be sent soon. "At least once" : ensure that the message arrives, but message duplication may occur "only once" : Ensure that the message arrives once. The advantage is to ensure that messages are delivered only once, while the disadvantage is high system overhead.Copy the code
  • Small transfer, very low overhead
  • Mechanisms for notifying interested parties of client outages using the Last Will and Testament features

2. mqtt broker

Is a server implementation based on MQTT protocol, namely MQTT server

3. mqtt client

Is a client implementation based on MQTT protocol.

MQTT server -EMQ installation

Official website to downloadwww.emqx.io/cn/download… After the download is complete (do not download enterprise version), unzip the package, open Windows command line window, CD to the program bin directory (the path cannot contain Chinese or space, otherwise it may error), start EMQ X:

G:\emqx-windows-v3.2.3\emqx\bin>emqx start G:\emqx-windows-v3.2.3\emqx> CD bin G:\emqx-windows-v3.2.3\emqx\bin>emqx_ctl Status Node '[email protected]' is started emqx 3.2.3 is running G:\emqx-windows-v3.2.3\emqx\bin>Copy the code

The browser is http://127.0.0.1:18083/#/, and the console visual page is displayed. The default user name and password are admin and public

3. Test using Paho

1. Download

The MQTT client graphical debugging tools Eclipse Paho Java all version download address repo.eclipse.org/content/rep…

2. Easy to use

  • Enter the exact IP and click Connect
  • “Subscribe” panel, add a topic and subscribe; “Publish” panel, fill in the message to publish and publish
  • Service quality: 0 at most once,1 at least once,2 just once. The specific decision depends on the project requirements. For example, the device keeps pushing messages and occasionally misses a unimportant option 0. It is hoped that every message can be received and no errors can be made (such as money or important information), then choose 1 and 2. Note: 2 is the most expensive. If not necessary, do not select 2.

Alternatively, you can use the Websocket tool in EMQ to publish a message with the subject test.The Paho client receives the sent message

3. Will debugging

About the function of the last wish: when the client with the last wish set abnormally exits while connecting to the MQTT server, the MQTT server will publish the last wish message of the client after the heartbeat timeout, and the client that subscribed to the last wish message will be notified. In general, this is to set up (app, device) and other customer unexpected exit (for example, disconnection) notification application server (this notification will be delayed, the latency depends on the heartbeat).

Actual project scenario: The application app server listened for the theme /logout message, and the client app connected to the MQTT server with the will theme set: /logout, message content {ID of current APP user}. When the app network of the user is disconnected, the MQTT server finds that the heartbeat of the connected APP client has stopped for a long time (related to the timeout time) and considers that the APP client has abnormally quit. It then posts the will message it set when it connected to the subject: /logout. The server has subscribed to the topic /logout message, so it receives the app user’S ID and can then do some logout processing.