mqtt 基础 - yanbin's Blog

mqtt 基础

yanbin posted @ 2015年3月15日 21:58 in Networking with tags Networking protocol mqtt , 9629 阅读
0. mqtt 是一种轻量级的 publish/subscribe 消息传输协议。
   这是一个应用层协议, 一种互联网通信协议,并不依赖底层网络协议。
   正因为其轻量级,所以在低电量设备的中广泛应用。mqtt 可以应用在更多的情景。
   常见的应用:facebook, 微博。
 
1. mqtt message 规格:
   MAX LENGTH: The Payload length valid values are between 0 and 268,435,455 (0xf,fff,fff).
   没错是 7 个f, 256 * 1024 * 1024 - 1.
 
2. mqtt 之 "pub/sub":
   mqtt 的基础原理是推送消息(publishing messages)和订阅主题(subscribing topics), 称为 "pub/sub".
   clients 连接到 broker ---> subscrib topics, 其他 clients 连接到 broker ---> publish message 到 topic,
   所有订阅了这个 topic 并且在线的 clients 都会收到这条 message.
 
   假如 clients publish message 到 topic, 但是订阅这个 topic 的 clients
   都没有在线, 或者还没有 client 订阅这个 topic. 这条 message 将会被消失(被丢弃了?是的被丢弃了)。
   可以设置 message 的 retained flag,再有其他 client 订阅到 topic 时会收到这条 message.
 
   1) 如果 publish message 时有 client 订阅到 topic, 这个条 message 还会保留吗?
   2) FIXME: 保留的 message 发送过之后还会再次发送到其他后来订阅到 topic 的 client 吗?
      **** retained message 保证每当 client 订阅 topic 时,会把 message 发送到 client.
            所有新在线的 client 都可以收到这条 message 直到 borker 进程重启。
      **** 即使 broker 进程重启,也有办法从硬盘存储中读取之前的消息,并且按照消息类型安排发送。
 
3. mqtt, broker:
   提供通用的简单的接口,接受其他 client 的连接。client 可以将 subscribedmessage 存入 database、
   发送到 twitter. 当然也可以从 database, twitter 读取 message 并推送。
   协议接口是简单通用的,任何实现的 client 都可以连接到任何实现的 broker.
 
4. mqtt, topic:
   1) Messages in MQTT are published on topics. There is no need to configure a topic,
      publishing on it is enough.
      topic 不需要配置,也不需要预先创建,有 client publish message 或这 subscribed topic 时由 broker 创建。
 
   2) topic 是层级结构,使用 '/' 作为分隔符, 就像是 Linux/Unix 的文件系统路径结构一样。
 
   3) 支持两个通配符(wildcard): '+' 和 '#'.
      '+': 通配层级结构中的一层。 a/b/+/d, a/b/c1/d   a/b/c2/d   a/b/c3/d 都会匹配。
      '#': 通配层级结构中所有剩余的层。 a/#, a/b/c1/d   a/b/c2/d   a/b/c3/d
 
   5) Zero length topic levels:
      Zero length topic levels are valid.
      0 长度 topic levels 表现出非现实的行为。可能会表现出 '+' 的行为,也可能表现出 '#' 的行为。
      具体行为要看 topic 的层级结构。
      例如有如下层级结构:
      a/b/c/d
      a/b/c1/d
      a/b/x/d
 
      a/b//d 相当于 a/b/+/d/
      a/b/c/ 相当于 a/b/c/+
      a/b/   相当于 a/b/#
 
      1) 如果给出的 0 length topic level 不是最后一层,那么就相当于 '+';
      2) 如果给出的 0 length topic level 是最后一层,要看实际 topic
         层级结构中, 这一层是不是最后一层,(a)如果是最后一层,那么就相当于 '+';
         (b) 如果不是最后一层,就相当于 '#'; 
 
5. Quality of Service.
   QoS 有三个级别:0,  1,   2
   mqtt QoS 并不是 TCP/IP 网络中报文的 QoS. 也不是 Linux 网络管理中的 QoS.
   mqtt QoS 定义了消息的发送和接收方式:
 
   1) Higher levels of QoS are more reliable, but involve higher latency and have
      higher bandwidth requirements.
      越高级别的 QoS 越可靠。相应的高级别的 QoS 带宽消耗大,延迟高
      因为要做握手或者发送接收确认。
 
   2) 三种级别的 QoS 消息发送/接收方式:
      0: The broker/client will deliver the message once, with no confirmation.
         提交 message 一次,不执行‘确认’操作。
 
      1: The broker/client will deliver the message at least once, with
         confirmation required.
         提交 message 至少一次,含有‘确认’请求,会执行'确认'操作。
 
      2: The broker/client will deliver the message exactly once by using a
         four step handshake.
         使用'4步握手'保证精确的提交一次 message.
   
   3)  Messages may be sent at any QoS level, and clients may attempt to
       subscribe to topics at any QoS level
 
       当 publish message 使用的 QoS 不同于 subscribe message 使用的 QoS 时,
       message 发送到 subscribed client 时,使用较低级别的 QoS.
       e.g.:
       (a) published message 使用 QoS 2, 一个 client0 订阅时使用 QoS 0, 那么
           message 提交给这个 client0 时使用 QoS 0.
       (b) 另一个 client1 订阅到这个 Topic 使用 QoS 2, 那么 message 提交给这个
           client 时使用 QoS 2.
       (c) 如果一个 client2 订阅时使用 QoS 2, 另一个 client3 publish message 
           使用 QoS 0, message 提交给 client2 时使用 QoS 0.
 
6. Retained Messages.
   1) 所有的 message 都可以设置为保留的。broker 保留 message 直到所有的订阅者接收 message.
   2) 而且当一个新的 client 订阅这个 topic,这个条 message 也会提交到这个 client.
   3) 可以用 retained message 来实现"last known good"机制。
 
 
7. Clean session / Durable connections
   1) client 连接到 broker 时设置 clean session flag 为 false, client 与 broker
      间建立了一个长连接(Durable connnecton); 当 client 断开与 broker 的连接时,
      任何 subscriptions 还有剩余的 QoS 为 1 或 2 的 messages,
      那么会保存这些 subscriptions/messages 直到再次连接。
 
   2) 如果 clean session flag 为 true,就相当于 'session start', 当 disconnect 时,
      会移除这个 client 所有的 subscriptions.
 
8. wills.
   will 是一种 message, 无异于其他 mqtt message。
   在 client 与 broker 建立连接时,client 告知 broker 一个 will message,用来
   在 client 与 broker 不正常断开连接时发送 will message 到一个特殊的 topic。
   这个特殊的 topic 是一个 system topic; system topic 有特殊命名,有特殊的访问权限,用于管理。
   will message 同样可以设置 QoS.
 
Avatar_small
Alyssa 说:
2022年12月14日 19:04

MQTT, or Message Queuing Telemetry Transport, is a publish/subscribe messaging protocol designed for lightweight M2M (machine-to-machine) communications. It is real estate services Goodyear frequently used in IoT applications where sensors and devices need to communicate with each other or with a central server. MQTT is a very simple protocol with a publish/subscribe message structure. Publishers send messages to a topic and subscribers receive messages from that topic.

Avatar_small
charlly 说:
2023年1月03日 15:35

MQTT is a popular lightweight messaging protocol designed for sending data over low-bandwidth networks. Its simple publish/subscribe messaging model makes it easy to develop client applications that osteoporosis prevention can send and receive messages from an MQTT broker.

Avatar_small
boardmodelpaper.com 说:
2024年1月09日 14:26

The Board model paper" typically refers to a sample or model question paper that is designed by educational boards or institutions for various exams. These papers serve as practice material for students preparing for exams, providing them with an idea of the question format, difficulty level, and the type of content that may be covered in the actual examination. boardmodelpaper.com Model papers are usually created for specific subjects or courses. They cover a range of topics and chapters that students are expected to have studied during the academic term. Students often use these educational board model papers as an integral part of their exam preparation strategy, helping them familiarize themselves with the exam pattern and refine their understanding of the subject matter.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter
Host by is-Programmer.com | Power by Chito 1.3.3 beta | © 2007 LinuxGem | Design by Matthew "Agent Spork" McGee