Article source: www.cnblogs.com/caca/p/mqtt…

MQTT – MQ Telemetry Transport

 

  • Lightweight machine-to-machine communication protocol.
  • The publish/subscribe pattern.
  • Based on the TCP/IP.
  • Supporting QoS.
  • Suitable for low bandwidth, unreliable connections, embedded devices, CPU and memory resources.
  • Is a relatively good Android notification push solution.
  • FacebookMessenger uses MQTT.
  • MQTT has the potential to become an important protocol for the Internet of Things.

 

The message body

 

 

 

MessageType

 

 

 

CONNECT

After the TCP connection is established, the Client sends a Request to the Server.

If no Response is received from the Server within a period of time, the socket is closed and a session connection is established again.

If a ClientID is already connected to the server, an old connection holding the same ClientID must be closed by the server before a new one can be established.

 

CONNACK

The Server sends Response.

0x00 Connection Accepted

0x01 Connection Refused: unacceptable protocol version

0x02 Connection Refused: identifier rejected

0x03 Connection Refused: server unavailable

0x04 Connection Refused: bad user name or password

0x05 Connection Refused: not authorized

 

PUBLISH PUBLISH message

Both clients and Servier can PUBLISH.

The Publish message should contain a TopicName(Subject/Channel), or subscribe keyword.

 

About Topic wildcards

/ : indicates levels, such as A /b, a/b/ C.

A /# matches a/, a/ B, a/ B/C.

A single # matches all.

A # and A /#/c are not allowed.

+ : matches a layer. For example, A /+ matches A/B, A/C, and does not match A/B/C.

A single + is allowed, a+ is not allowed, a/+/b is not allowed

 

PUBACK’s confirmation after the announcement

When QoS is 1, the Server sends the confirmation to the Client (the Client deletes the confirmation after receiving the confirmation), and the subscriber sends the confirmation to the Server.

 

PUBREC / PUBREL / PUBCOMP

QoS = 2

1. Server->Client released PUBREC (received);

2. Client->Server releases PUBREL;

3. Server->Client publishes PUBCOMP (completed), Client deletes MSG;

Subscribers also issue similar process confirmations to the Server.

 

PINGREQ/PINGRES Indicates the heartbeat

The Client is responsible for sending the KeepAliveTime duration to the Server. Within a period of time, PINGREQ is sent, and the Server sends PINGRES confirmation.

If the Server does not receive a PINGREQ within 1.5 hours, it disconnects.

The Client does not receive PINGRES within one period and is disconnected. Procedure

Typically, the duration is set to a few minutes. The maximum number is 18hours. 0 indicates that the connection is not interrupted.

 

 

QoS: Quality of Service

 

 

QoS=0: indicates a maximum of one time, which may be repeated or lost.

 

QoS=1: at least once.

Client[Qos=1,DUP=0/* Number of iterations */,MessageId=x] –>PUBLISH–> Server Stores Message, publishes, deletes Message, and sends PUBACK to Client

After receiving the PUBACK, the Client deletes the Message. If PUBACK is not received, set DUP++ to resend, and the Server republishes, so it is possible to re-send messages.

 

QoS=2: only once, ensuring that the message arrives only once (for strict billing systems).

 

 

Clean Session

 

If the value is false(flag=0), the Server should save the subscription information of the Client after the Client is disconnected.

If the value is true(flag=1), the Server should immediately discard any session status information.

 

 

 

Refs

Public.dhe.ibm.com/software/dw…

\