Keep Alive Mechanism in MQTT Protocol
Why you need Keep Alive
MQTT protocol is carried on top of the TCP protocol, and the TCP protocol is connection-oriented, providing a stable and orderly byte stream function between the two sides of the connection. However, in some cases, TCP may have half-connection problems. The so-called semi-connection means that the connection of one party has been disconnected or not established, while the connection of the other party is still maintained. In this case, the semi-connected party may continue to send data to the peer, which obviously never reaches the peer. In order to avoid the communication black hole caused by the semi-connection, the MQTT protocol provides the Keep Alive mechanism, so that the client and the MQTT server can determine whether there is a current semi-connection problem and close the corresponding connection.
The mechanism and use of Keep Alive
enable
When the client creates a connection with the MQTT Broker, as long as the Keep Alive variable header field in the connection request protocol packet is set to a non-zero value, the Keep Alive mechanism can be enabled between the two communicating parties. Keep Alive is an integer ranging from 0 to 65535, representing the maximum interval between two MQTT protocol packets sent by the client.
After the Broker receives the connection request from the client, it will check the value of the Keep Alive field in the variable header. If there is a value, the Broker will enable the Keep Alive mechanism.
In the MQTT 5.0 standard, the concept of Server Keep Alive was introduced, allowing the Broker to choose to accept the Keep Alive value carried in the client request according to its own implementation and other factors, or to override this value. If the Broker chooses to override this value, it needs to set the new value in the Server Keep Alive field of the connection confirmation packet ( CONNACK ). If the client reads Server Keep Alive in the connection confirmation packet, it needs to Use this value to overwrite your previous value of Keep Alive .
Keep Alive mechanism process
Client process
After the connection is established, the client needs to ensure that the interval between sending any two MQTT protocol packets does not exceed the value of Keep Alive . If the client is currently idle and has no packets to send, it can send PINGREQ protocol Bag.
When the client sends PINGREQ protocol packet, the Broker must return a PINGRESP protocol packet. If the client does not receive the server's PINGRESP protocol packet within a reliable time, it means that there is a current protocol packet of 16230648a7600c PINGRESP 16230648a7600 The broker has been offline or a network failure has occurred. At this time, the client should close the current connection.
Broker process
After the connection is established, if the Broker does not receive any packets from the client within 1.5 times of Keep Alive , it will consider that there is a problem with the connection with the client, and the Broker will disconnect from the client. end connection.
If the Broker receives the PINGREQ protocol packet from the client, it needs to reply with a PINGRESP protocol packet for confirmation.
Client takeover mechanism
When there is a semi-connection in the Broker, if the corresponding client initiates a reconnection or a new connection at this time, the Broker will start the client takeover mechanism: close the old semi-connection, and then establish a new connection with the client.
This mechanism ensures that the client will not be unable to reconnect due to the semi-connection problem in the Broker.
How to use Keep Alive in EMQX
In EMQX , users can customize the behavior of the Keep Alive mechanism through configuration. The main configuration fields are:
zone.${zoneName}.server_keepalive
server_keepalive 类型 默认值 整型 无
If this value is not set, EMQX will control the behavior of Keep Alive according to the value of Keep Alive when the client creates a connection.
If this value is set, the Broker will force the Keep Alive mechanism to be enabled for all connections under the zone, and will use this value to override the value in the client connection request.
zone.${zoneName}.keepalive_backoff
keepalive_backoff 类型 默认值 浮点数 0.75
The MQTT protocol requires the Broker to be disconnected from the client if it does not receive any protocol packets from the client within 1.5 times Keep Alive .
In EMQX, we introduced a keepalive backoff, and exposed this coefficient through configuration, so that users can more flexibly control the behavior of Keep Alive on the Broker side.
After introducing the backoff coefficient, EMQX calculates the maximum timeout time by the following formula:
Keepalive * backoff * 2
backoff default value is 0.75, so the behavior of EMQX fully conforms to the MQTT standard if the user does not modify this configuration.
For more related content, please refer EMQX configuration document .
Set Keep Alive when WebSocket is connected
EMQX supports client access through WebSocket. When the client uses WebSocket to initiate a connection, it only needs to set the keepalive value in the connection parameters. For details, see Using WebSocket to connect to the MQTT server .
Epilogue
This article introduces the mechanism of Keep Alive in the MQTT protocol and the use of Keep Alive in EMQX. Developers can use this feature to ensure the stability of MQTT connections and build more robust upper-layer IoT applications.
Copyright statement: This article is original by EMQ, please indicate the source when reprinting.
Original link: https://www.emqx.com/zh/blog/mqtt-keep-alive
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。