Xiaopei's DokuWiki

These are the good times in your life,
so put on a smile and it'll be alright

User Tools

Site Tools


it:mqtt

MQTT - MQ Telemetry Transport (messaging protocol)

Telemetry: 遥感勘测, 自动测量记录传导

特点

  • 使用发布/订阅消息模式,提供一对多的消息发布,解除应用程序耦合。
  • 对负载内容屏蔽的消息传输。
  • 使用 TCP/IP 提供网络连接。
  • 有三种消息发布服务质量:
    • QoS 0 - At most once delivery “至多一次”,消息发布完全依赖底层 TCP/IP 网络。会发生消息丢失或重复。这一级别可用于如下情况,环境传感器数据,丢失一次读记录无所谓,因为不久后还会有第二次发送。
    • QoS 1 - At least Once Delivery “至少一次”,确保消息到达,但消息重复可能会发生。
    • QoS 2 - Exactly once delivery “只有一次”,确保消息到达一次。这一级别可用于如下情况,在计费系统中,消息重复或丢失会导致不正确的结果。
  • 小型传输,开销很小(固定长度的头部是 2 字节),协议交换最小化,以降低网络流量。
  • 使用 Last Will 和 Testament 特性通知有关各方客户端异常中断的机制。 :?:
  • 一个 SERVER 是既可以当 SUBSCRIBER (接受 devices 的信息) 又可以当 PUBLISHER (发给第三方)么?
    → server 既不是 subscriber 又不是 publisher, 而是一个 “存放消息” 的 broker
  • 为什么需要 mosquitto,而不是业务服务 new MQTT_Server().listen()
    → mosquitto 是 MQTT server 的一种选择, 业务服务也可以自己 new MQTT_Server()

定义 of V3.1

整体包括:fixed header + 可选的 variable header + payload

fixed header

bit 7 6 5 4 3 2 1 0
byte 1 Message Type DUP flagQoS level RETAIN
byte 2 Remaining Length

All data values are in big-endian order: higher order bytes precede lower order bytes.

Message Type 可分为以下几类:

  • 建立和中断连接:CONNECT, CONNACK, DISCONNECT
  • 发布消息:PUB*
  • 订阅:*SUBSCRIBE
  • 接收消息:*SUBBACK
  • PING:PING*

Message Type 详细定义如下:

Mnemonic Enumeration Description
Reserved 0 Reserved
CONNECT 1 Client request to connect to Server
CONNACK 2 Connect Acknowledgment
PUBLISH 3 Publish message
PUBACK 4 Publish Acknowledgment
PUBREC 5 Publish Received (assured delivery part 1)
PUBREL 6 Publish Release (assured delivery part 2)
PUBCOMP 7 Publish Complete (assured delivery part 3)
SUBSCRIBE 8 Client Subscribe request
SUBACK 9 Subscribe Acknowledgment
UNSUBSCRIBE 10 Client Unsubscribe request
UNSUBACK 11 Unsubscribe Acknowledgment
PINGREQ 12 PING Request
PINGRESP 13 PING Response
DISCONNECT 14 Client is Disconnecting
Reserved 15 Reserved

flags 意义如下:

  • DUP: Duplicate delivery,set when the client or server attempts to re-deliver a PUBLISH, PUBREL, SUBSCRIBE or UNSUBSCRIBE message. This applies to messages where the value of QoS is greater than zero (0), and an acknowledgment is required. When the DUP bit is set, the variable header includes a Message ID.
  • QoS: Quality of Service
  • RETAIN: 保留标志
    • 客户端可以 PUBLISH 带 RETAIN 的消息给服务器,服务器收到消息后,会保留该消息(即使重启也应保留),并发给所有 SUBSCRIBER
    • SERVER 收到该消息后新连接的 SUBSCRIBER 也会收到 最后一条 RETAIN 信息(带 RETAIN flag)。SUBSCRIBER 可据 RETAIN 判断是新消息 or 老消息
    • SERVER 收到 a message with a zero-length payload and the Retain flag set on the same topic 后可删除 RETAIN 包
  • Remaining Length: Represents the number of bytes remaining within the current message, including data in the variable header and the payload.

variable header

payload

流程 flow

QoS0
QoS1
QoS2

broker (server)

Topic wildcards

  • /, Topic level separator
  • #, Multi-level wildcard
    finance/#
    
    matches:
      finance 
      finance/stock/ibm
      finance/stock/ibm/closingprice
      finance/stock/ibm/currentprice
    
    finance# is not valid
  • +, Single-level wildcard
    finance/stock/+
    
    matches:
      finance/stock/ibm
      finance/stock/xyz
    
    not matches:
      finance
      finance/stock/ibm/closingprice

参考

it/mqtt.txt · Last modified: 2014/08/15 11:02 by admin