<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by eddix on Medium]]></title>
        <description><![CDATA[Stories by eddix on Medium]]></description>
        <link>https://medium.com/@eddix?source=rss-534cb6af847c------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*XM6EfapMmBrqt33rzKtUSQ.png</url>
            <title>Stories by eddix on Medium</title>
            <link>https://medium.com/@eddix?source=rss-534cb6af847c------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 20:30:15 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@eddix/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Retransmission mechanism of MQTT]]></title>
            <link>https://medium.com/@eddix/retransmission-mechanism-of-mqtt-a4fc10578ab?source=rss-534cb6af847c------2</link>
            <guid isPermaLink="false">https://medium.com/p/a4fc10578ab</guid>
            <category><![CDATA[mqtt]]></category>
            <dc:creator><![CDATA[eddix]]></dc:creator>
            <pubDate>Wed, 07 Dec 2016 03:22:46 GMT</pubDate>
            <atom:updated>2016-12-07T03:22:46.529Z</atom:updated>
            <content:encoded><![CDATA[<p>There are 3 levels QoS in MQTT, the retransmission mechanism only work under QoS 1 or QoS 2.</p><p>I will use <em>sender</em> and <em>receiver</em> instead of <em>publisher</em>, <em>subscriber</em> and <em>broker</em> in the following.</p><p>When publish events occur, the sender is the publisher and the receiver is the broker.</p><p>When subscribe events occur, the sender is the broker, and the receiver is the subscriber.</p><p>Under QoS 0, there is no retransmission. Sender will drop the message once it sent the message package, no matter whether the peer receive it.</p><p>Under QoS 1, MQTT use PUBLISH and PUBACK commands.</p><p>Sender should maintain a queue, for example a vector&lt;Message*&gt; in the memory and the data in queue may be also persisted on the disk for restart use. Since sender send the message at the front of the queue, it should wait the receive send PUBACK back, before sender receive the PUBACK package, it should keep the message not be lost.</p><p>It works like this:</p><blockquote>sender — PUBLISH message of mid001 -&gt; receiver</blockquote><blockquote>sender &lt;- PUBACK message id of mid001 — receiver</blockquote><blockquote>sender remove mid001</blockquote><p>If sender hadn’t receive the PUBACK package and the connection was closed or terminated, sender would keep the mid001 message in the queue, once the receiver connect again, sender will send the message mid001 again.</p><p>Obviously, if the connection was broken before the receiver got the message mid001, receiver will got the message mid001 in the next connection, and it will only got the message mid001 once. BUT if the connection was broken after the receiver got the PUBLISH package, and before the sender receive the PUBACK package, when the connection was established again, sender will send mid001 again, in the case, receiver will got the message mid001 twice(and the PUBACK package may lose again in the second connection, then receiver will got the message thrice in the 3rd connection).</p><p>That’s why QoS 1 will ensure the receiver get one message at least once.</p><p>To make one message transmit extract once, MQTT has QoS 2.</p><p>QoS 2 is more complex and costly. It use PUBLISH, PUBREC, PUBREL and PUBCOMP 4 command. The data formats are:</p><blockquote>PUBLISH:MessageID,MessageBody</blockquote><blockquote>PUBREC:MessageID</blockquote><blockquote>PUBREL:MessageID</blockquote><blockquote>PUBCOMP:MessageID</blockquote><p>It works like this:</p><blockquote>sender(mid001 <strong>in</strong> queue) — PUBLISH -&gt; receiver</blockquote><blockquote>sender(mid001 <strong>in</strong> queue) &lt;- PUBREC — receiver(mid001 <strong>in</strong> buffer)</blockquote><blockquote>sender(mid001 <strong>in</strong>-fly) -&gt; PUBREL — receiver(mid001 <strong>in</strong> buffer)</blockquote><blockquote>sender(mid001 <strong>in</strong>-fly) &lt;- PUBCOMP -receiver(mid001 was used)</blockquote><blockquote>sender remove mid001</blockquote><p>The retransmission mechanism is:</p><ol><li>Receiver won’t really active the mid001 message after the first time it was received. It just send a PUBREC package with the message id to the sender to tell the peer message mid001 was copied. Receiver will only consume the message after it got the PUBREL package.</li><li>The transmission divide into two steps, the first step the message was copied in the receiver’s buffer, but not really be used, the second step the id(a 16-bit number) of message was transmit, and make the message really active.</li><li>In the 4-times communication, if the connection was broken in one command, the peers will try it again in the next time they connect.</li><li>If the connection was broken in the PUBLISH command, sender will send the message again.</li><li>if the connection was broken in the PUBREC command, because sender didn’t know whether the PUBLISH command was received, it will send a new message with the same id, the receiver will find there is a duplicate message in buffer then drop either one and send a PUBREC command again. Once sender got the PUBREC command, it will mark the message mid001 as <strong>in-fly</strong> message and send a PUBREL command.</li><li>if the connection was broken in the PUBREL command, sender will send a new PUBREL command in the next connection. Because sender knew the receiver has the message body, it just need to active it.</li><li>If the connection was broken in the PUBCOMP command, sender will send a new PUBREL command in the next connection, because sender will find there is a in-fly message. Receiver got the PUBREL command but find there is no such a message in its buffer, receiver will do nothing but send a PUBCOMP command to complete the communication for message mid001.</li><li>In the 2 steps transmission, a message will be active only the 4-time communication complete. This mechanism makes the message will be active exactly once.</li></ol><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=a4fc10578ab" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>