如何发现存在的Topic
The subscriber does not know which topics are available. We have seen many deployments where far more than 100.000+ topics are used in the system and sending these topic lists to subscribers would be overkill. The protocol itself does not define such a topic discovery. We have seen these two popular solutions in deployments:
* Subscribe to a custom $SYS topic which contains all available topics for the client (e.g. $SYS/{client_id}/topics). You can learn about hooking in custom $SYS topics
Debug调试的时候的信息的含义
“received PUBLISH (d0, q0, r0, m0, ‘get/123’, … (70 bytes))”
the fields have the following meaning:
d0 => duplicate flag: false
q0 => QoS: 0
r0 => retained: false
m0 => messageID: 0
想要收到有订阅者带走了消息?
MQTT本身无法做到,可以从业务层次实现,比如往另一个Topic里面推送数据
MQTT is a data agnostic protocol that can transfer any kind of information. For that sound files like in your case need to be first converted in byte array before the publish. The subscriber (receiver) needs than to convert back the received byte array.
retain消息不会影响当前已经连接的客户端
也就是只有新连接的客户端才会收到 retain 消息,已经连接的不会收到
One retained message for a specific topic (The last message published on the topic, regardless of the payload) can be stored by the broker so that any new subscribers on that specific topic will be able to get that message. This does not in any way influence the message flow of currently connected subscribers.
如何识别Payload里面的消息类型呢?是否有HTTP那样的Content-Type标志位?
没有任何标志位,可以通过Topic本身的分类来设置不同的消息类型,MQTT是消息无关的协议,不关心自己Payload里面的数据内容
cleanSesson标志位的作用
如果cleanSession=false 那么一旦客户端断开,则对应的Topic
QoS服务降级
只会降级不会升级,也就是会用最低服务质量(发布者发布的消息QoS和订阅者的QoS要求会使用最低质量服务)
如何保证订阅者可以收到所有的消息,即使离线?
All messages will be sent to client B automatically after the reconnect if all of the following conditions are met:
– Client B has been connected before with the clean session flag is false and has subscribe to the topic, where the messages arrive
– All messages were sent with quality of service greater than 0
You can read more on how the queuing works here: http://www.hivemq.com/blog/mqtt-essentials-part-7-persistent-session-queuing-messages
When looking at retained messages only the last message will be persisted on the topic. It will be delivered to a newly subscribing client, right after subscribing.
如何删除Retain消息?
发送一个含有retain标志位但是payload为空的数据
A zero byte payload message will not be forwarded to subscribers. It’s sole purpose is to clear a retained message on a topic.
如何再次收到retain消息?
断开重连或者取消订阅后重新订阅
The only way to “update” the retained message for a client is to re-subscribe to the topic.
LWT(Last Will Testament) 遗嘱消息什么时候删除?
当客户端Graceful disconnect的时候,它的遗嘱消息就会删除
LWT最佳实践
配合Retain消息使用,比如Retain设置设备在线,这样所有的订阅者都能查询设备状态,但是在LWT中则定义离线,一旦设备离线则broker则会发送离线消息
如果客户端的ID相同(client_id)会怎么样?
client_id用于识别会话,如果相同,这个问题很严重,会导致mqtt认为同一个客户端尝试重新连接,于是会关闭之前的连接然后再新建连接,对于死循环的客户端来说会导致服务器和客户端之间不断的断开和连接,非常消耗资源的死循环流程,因此没有特殊情况,可以不指定clientid,因为大部分库都能够良好的生成随机ID。