Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
MQTT 通訊協定
Search
Bernie
September 27, 2012
Technology
1
5.2k
MQTT 通訊協定
Lightweight Communication Protocol Based on Application Layer of OSI Model
Bernie
September 27, 2012
Tweet
Share
Other Decks in Technology
See All in Technology
Rustから学ぶ 非同期処理の仕組み
skanehira
1
150
AIエージェント開発用SDKとローカルLLMをLINE Botと組み合わせてみた / LINEを使ったLT大会 #14
you
PRO
0
130
バイブスに「型」を!Kent Beckに学ぶ、AI時代のテスト駆動開発
amixedcolor
2
580
Django's GeneratedField by example - DjangoCon US 2025
pauloxnet
0
150
なぜテストマネージャの視点が 必要なのか? 〜 一歩先へ進むために 〜
moritamasami
0
230
はじめてのOSS開発からみえたGo言語の強み
shibukazu
3
890
DroidKaigi 2025 Androidエンジニアとしてのキャリア
mhidaka
2
370
まずはマネコンでちゃちゃっと作ってから、それをCDKにしてみよか。
yamada_r
2
120
20250910_障害注入から効率的復旧へ_カオスエンジニアリング_生成AIで考えるAWS障害対応.pdf
sh_fk2
3
260
品質視点から考える組織デザイン/Organizational Design from Quality
mii3king
0
210
Android Audio: Beyond Winning On It
atsushieno
0
2.4k
スマートファクトリーの第一歩 〜AWSマネージドサービスで 実現する予知保全と生成AI活用まで
ganota
2
270
Featured
See All Featured
The Web Performance Landscape in 2024 [PerfNow 2024]
tammyeverts
9
810
What’s in a name? Adding method to the madness
productmarketing
PRO
23
3.7k
The Invisible Side of Design
smashingmag
301
51k
The Straight Up "How To Draw Better" Workshop
denniskardys
236
140k
"I'm Feeling Lucky" - Building Great Search Experiences for Today's Users (#IAC19)
danielanewman
229
22k
Connecting the Dots Between Site Speed, User Experience & Your Business [WebExpo 2025]
tammyeverts
8
530
Agile that works and the tools we love
rasmusluckow
330
21k
Building a Modern Day E-commerce SEO Strategy
aleyda
43
7.6k
Balancing Empowerment & Direction
lara
3
620
Reflections from 52 weeks, 52 projects
jeffersonlam
352
21k
Why You Should Never Use an ORM
jnunemaker
PRO
59
9.5k
Documentation Writing (for coders)
carmenintech
74
5k
Transcript
MQTT通訊協定 Lightweight Communication Protocol Based on Application Layer of OSI
Model @Bernie
二、通訊協定/基本概論 MQTT, Message Queue Telemetry Transport ◦ 中文翻譯: 訊息序列遙測傳輸技術
◦ 起源於: 西元1999年 IBM 建立 ◦ 創始人: Andy Stanford-Clark and Arlen Nipper ◦ 版權: 開放協定,免授權金 ◦ 特色: 即時傳遞的輕量協定專用於低頻寬或昂 貴連線的小裝置之間將換訊息,且具備三個服 務品質層級 ◦ 傳輸: 使用 TCP 層 ◦ 版本: 目前規格為 v3.1
二、通訊協定/協定格式 Fixed Header Variable Header Message Payload
Fixed Varable Payload 2 bytes 2~12 bytes 127 bytes
二、通訊協定/協定格式(續) Fixed Header ◦ 第一個 Byte 內容說明 Message
Type (4~7 bites) DUP: 重新發送 PUBLISH, PUBREL, SUBSCRIBE, UN SUBSCRIBE時,且QoS數值大於0,並於可變標頭部 分置入Message ID QoS: 發送PUBLISH時採用 RETAIN:發送PUBLISH時採用,在訊息尚未發送到訂 閱者之前Server會保留訊息,直到訂閱者收到後刪除 ◦ 第二個 Byte 為剩餘內容之長度
二、通訊協定/協定格式(續) Message Type
二、通訊協定/協定格式(續) Variable Header ◦ 1. Protocol name (CONNECT) ◦
2. Protocol version (CONNECT)
二、通訊協定/協定格式(續) Variable Header ◦ 3. Connect flags (CONNECT)
User Name Flag/Password Flag: 發送CONNECT訊息時, 是否需要附帶欄位資訊 Will Retain: Client意外斷開連線時,Server是否保留發佈 的Will Message Will QoS: Client意外斷開連線時,Server將依照QoS層級 發送訊息 Will Flag: Client意外斷開連線時,Server是否發佈訊息 Clean Session: 設定Client是否會收到過去時間的發佈訊 息
二、通訊協定/協定格式(續) Variable Header ◦ 4. Keep Alive timer (CONNECT)
Keep Alive: 將時間區段之數值填入,使得 Server 能夠偵測 Client 的網路連線
二、通訊協定/協定格式(續) Variable Header ◦ 5. Connect return code (CONNACK)
Connection Accepted: 0x00 Connection Refused: unacceptable protocol version: 0x01 Connection Refused: identifier rejected: 0x02 Connection Refused: server unavailable: 0x03 Connection Refused: bad user name or password: 0x04 Connection Refused: not authorized: 0x05 Reserved for future use: 保留
二、通訊協定/協定格式(續) Variable Header ◦ 6. Topic name (PUBLISH)
最多支援到 32767 Characters ◦ 7. Message Identifiers
二、通訊協定/協定格式(續) Message Payload (CONNECT,SUBSRIBE,SUBACK) ◦ Client Identifier ◦ Will
Topic ◦ Will Message ◦ User Name ◦ Password ◦ Others (放置額外的資料數值)
二、通訊協定/協定格式(續) QoS, Qualities of Service ◦ QoS Lv. 0:
至多一次傳送 (non-persistent) 沒有重複發送機制 ◦ QoS Lv. 1: 至少一次 (persistent) Client Variable Header 帶有 Message ID Server 利用 PUBACK 控制流程 訊息重複發送採用 DUP bit 控制 ◦ QoS Lv. 2: 剛好一次 (persistent) Client/Server 利用 PUBREC/PUBREL 控制流程 Server 利用 PUBCOMP 確保傳輸過程
二、通訊協定/封包種類 建立/取消連線 ◦ CONNECT ◦ CONNACK ◦ DISCONNECT ◦
PINGREQ ◦ PINGRESP 發佈訊息 PUBLISH PUBACK PUBREC PUBREL PUBCOMP (取消)訂閱訊息 SUBSCRIBE SUBACK UNSUBSCRIBE UNSUBACK
二、通訊協定/通訊流程範例 建立連線 ◦ Client 主動連線,Server 接受連線 發佈訊息 ◦
Client (Publisher)發佈 QoS=0 之訊息到 Server 主動訂閱 ◦ Client (Subscriber) 訂閱 Server 所提供的主 題
二、通訊協定/通訊流程範例(續) 建立連線: Client 主動連線,Server 接受 連線 Client Server SYN
Seq=0 Seq=0, Ack=1 SYN, ACK ACK Seq=1, Ack=1 Seq=1, Ack=1 PSH, ACK Seq=1, Ack=26 ACK PSH, ACK ACK Seq=1, Ack=26 Seq=26, Ack=5
二、通訊協定/通訊流程範例(續) 建立連線: Client 主動連線,Server 接受 連線 Client Server Seq=1,
Ack=1 CONNECT CONNACK Seq=1, Ack=26 Seq=26, Ack=5
二、通訊協定/通訊流程範例(續) 發佈訊息: Client (Publisher)發佈 QoS=0 之訊息到 Server Client Server
Seq=1, Ack=1 PUBLISH Seq=29, Ack=1 Client PUBLISH PUBLISH Seq=1, Ack=32
二、通訊協定/通訊流程範例(續) 主動訂閱 ◦ Client (Subscriber) 訂閱 Server 所提供的主 題
Client Server SUBSCRIBE Seq=1, Ack=18 SUBACK Seq=1, Ack=1 82 > 1010(Message Type: SUBSCRIBE) 0010(QoS Level: 1)
補充說明
封包資料格式-剩餘長度 Fixed Header ◦ 第二個 Bytes (以上) 內容說明
至少 1 Bytes 存放 Variable Header 以及 Payload 長度 Fixed Varable Payload 5 bytes 2~12 bytes 256 MB
封包資料格式-剩餘長度(續) Remaining Length ◦ 每一組 Byte 中的排列方式 0~7
bits 為保存壓縮內容 8 bit 為 continuation bit ◦ 增加的 Byte 算在 Fixed Header 中,不影響 Variable Header 之長度 ◦ Payload 最大可到 256 MB (268435455 Bytes)