MQT 学习

MQTT 学习笔记。



MQTT example



[MQTT Essentials – A Lightweight IoT Protocol](../resources/MQTT Essentials – A Lightweight IoT Protocol.epub) — Good Book !!! 🐂

MQTT 是种基于订阅-发布机制的异步通信技术,核心是解耦

解耦(Decouple)是 [MQTT Essentials – A Lightweight IoT Protocol](resources/MQTT Essentials – A Lightweight IoT Protocol.epub) 内反复多次提到的一个词 。

发布/订阅模式(Publish-subscribe pattern)



  • 发布者与订阅者不必了解彼此,只要认识同一个消息代理即可。
  • 发布者和订阅者不需要交互,发布者无需等待订阅者确认而导致锁定。
  • 发布者和订阅者不需要同时在线,可以自由选择时间来消费消息。




  • building-b/floor-5:代表B楼5层的设备。
  • +/floor-5:代表任何一个楼的5层的设备。
  • building-b/*:代表B楼所有的设备。


遗言机制(Last will)




I’m surely missing something about how the whole MQTT protocol works, as I can’t grasp the usage pattern of Last Will Testament messages: what’s their purpose?
One example I often see is about informing that a device has gone offline. It doesn’t make very much sense to me, since it’s obvious that if a device isn’t publishing any data it may be offline or there could be some network problems.

So, what are some practical usages of the LWT? What was it invented for?

LWT messages are not really concerned about detecting whether a client has gone offline or not (that task is handled by keepAlive messages). LWT messages are about what happens after the client has gone offline.

The analogy is that of a real last will: If a person dies, she can formulate a testament, in which she declares what actions should be taken after she has passed away. An executor will heed those wishes and execute them on her behalf. The analogy in the MQTT world is that a client can formulate a testament, in which it declares what message should be sent on it’s behalf by the broker, after it has gone offline.

A fictitious example:

I have a sensor, which sends crucial data, but very infrequently. It has formulated a last will statement in the form of [topic: ‘/node/gone-offline’, message: ‘:id’], with :id being a unique id for the sensor. I also have a emergency-subscriber for the topic ‘node/gone-offline’, which will send a SMS to my phone every time a message is published on that channel.

During normal operation, the sensor will keep the connection to the MQTT-broker open by sending periodic keepAlive messages interspersed with the actual sensor readings. If the sensor goes offline, the connection to the broker will time out, due to the lack of keepAlives.

This is where LWT comes in: If no LWT is specified, the broker doesn’t care and just closes the connection. In our case however, the broker will execute the sensor’s last will and publish the LWT-message ‘/node/gone-offline: :id’. The message will then be consumed to my emergency-subscriberand I will be notified of the sensor’s ID via SMS so that I can check up on what’s going on.

In short:

Instead of just closing the connection after a client has gone offline, LWT messages can be leveraged to define a message to be published by the broker on behalf of the client, since the client is offline and cannot publish anymore.

Furthermore, keepAlive messages are only sent and received by the broker and there’s the main difference with the Last Will: anyone can suscribe to this topic and be aware of the client going offline


page 69

Client            Server
|--- CONNECT --->|
|<-- CONNACK ----|