Mqtt协议——cleansession、retain、will、Shared Subscription

Foreword

Prior to read some articles about Mqtt agreement, I do not read the notes, it took so long to forget a lot. Recently just do related things, need to use, remember on the way down.

text


cleansession

Clear Session

Set in the client server connection.

Values: true / false

Role: The server must continue to store after the client disconnects / hold subscription status of the client, including the following states

  • Subscribe to news Qos1 storage and Qos2 message is sent when a client re-subscribe
  • During the service to send messages to the correct client connection is lost resulting in the failure to send a message

retain

Endurance news.

Think about the following scenario, you have a temperature and humidity sensor every few bell to send a message to a topic, then you have a new client subscribes to the theme, then it we going to wait a few clock to get the message right ? Can you get a subscription to send a message on it? The answer is yes

Values: true / false

Role: indicates that the message has been sent by the need to persist (without server restarts influence), only to be sent to current subscribers and new subscribers after this Topic name subscribers will soon get the push.

Note: new to subscribe to this topic name of the subscriber refers to the new clientIDuser, which means that if you are called 12345user in 同一次的连接repeated subscription and subscription once the words are the same; (unless you break the ① reconnected, ② unsubscribe and then resubscribe, so can get retain the message again.)

If you want to cancel the message, then send a message retian space to the subject like a ~


will

Wishes messages

Think about the following scenarios, your device sends online messages to the server suddenly exploded, it has not had time to say it exploded and server died, so do not let us think that it will be online, but in fact it has Bye. There is no way for the client to inform the server abnormal disconnection after it? Yes, the message is to use the last wish

Agreed upon establishing a connection with the service side of good wishes message, the server stores the message when the client wishes abnormal disconnection will send a message to the appointment to the topic, again, it can also be set retian.


share subscription

Share subscription (need to support the broker mqtt5.0)

Hypothesis 4 subscribers subscribed to the same theme, this time there publisher sends mission to the topic, if the past is four subscribers will receive this task, and then to deal with; but now just want to each message if there is a subscribers can handle, then use the share subscription on it

Share subscription for the scene should be the producer of data far beyond the number of data consumers, and the same data (message) need only be processed once any one of the consumer . It is to achieve the number of consumers process messages balanced load .


Here Insert Picture Description
Create a shared two ways to subscribe:

Subject prefix Examples
$queue/:topic sub $queue/up/data
$share/:group/:topic sub $share/group/up/data

Share subscription consists of three parts:

  • Static shared identifier ($ queue with $ share)
  • Group identifier (optional)
  • Actually received the message theme

$ Queue and $ share of the differences:

Theme of $ queue after all the messages sent to the client in turn,

After $ share, you can add different groups, such as:

  • $share/group_1/topic
  • $share/group_2/topic
  • $share/group_3/topic

When the broker sends a message to the topic, each group receives the message and sends the message to a random device in this group

Published 48 original articles · won praise 56 · views 20000 +

Guess you like

Origin blog.csdn.net/zhetmdoubeizhanyong/article/details/104871483