{"id":41,"date":"2023-09-01T20:26:16","date_gmt":"2023-09-01T19:26:16","guid":{"rendered":"https:\/\/codeblog.xyz\/?p=41"},"modified":"2023-10-23T21:16:05","modified_gmt":"2023-10-23T18:16:05","slug":"mqtt-message-queuing-telemetry-transport","status":"publish","type":"post","link":"https:\/\/codeblog.xyz\/?p=41","title":{"rendered":"MQTT &#8211; Message Queuing Telemetry Transport"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">What is MQTT?<\/h4>\n\n\n\n<p>MQTT stands for Message Queuing Telemetry Transport. It&#8217;s a lightweight publish\/subscribe messaging protocol designed for low-bandwidth, high-latency, or unreliable networks. MQTT has found its way into various kinds of systems like IoT devices, home automation, and even mobile apps.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">When and by whom it was developed:<\/h4>\n\n\n\n<p>MQTT was originally developed by Andy Stanford-Clark of IBM and Arlen Nipper of Arcom (now Cirrus Link) in 1999 to connect oil pipelines over satellite links.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">First Usage:<\/h4>\n\n\n\n<p>The protocol&#8217;s first use case was for the monitoring of an oil pipeline through the desert to enable reliable communications over problematic networks.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Pros and Cons:<\/h4>\n\n\n\n<p><strong>Pros<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Low Bandwidth<\/strong>: Efficient packet size is perfect for conditions where network bandwidth is limited.<\/li>\n\n\n\n<li><strong>Quality of Service Levels<\/strong>: Offers three levels of QoS for message delivery:\n<ol class=\"wp-block-list\">\n<li>At most once (0)<\/li>\n\n\n\n<li>At least once (1)<\/li>\n\n\n\n<li>Exactly once (2)<\/li>\n<\/ol>\n<\/li>\n\n\n\n<li><strong>Last Will and Testament<\/strong>: Allows for informing other clients about a sudden disconnect.<\/li>\n\n\n\n<li><strong>Retained Messages<\/strong>: Stores the last relevant message for a topic for a while.<\/li>\n<\/ul>\n\n\n\n<p><strong>Cons<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Not HTTP-based<\/strong>: MQTT isn&#8217;t web-friendly like REST.<\/li>\n\n\n\n<li><strong>Broker Required<\/strong>: Requires a centralized broker to manage publications and subscriptions.<\/li>\n\n\n\n<li><strong>Limited Security<\/strong>: The standard doesn&#8217;t define fine-grained security mechanisms (though SSL\/TLS is often used).<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">When to Use It:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>IoT Devices<\/strong>: Resource-constrained devices like sensors.<\/li>\n\n\n\n<li><strong>Real-time Analytics<\/strong>: Stock market dashboards or live sports updates.<\/li>\n\n\n\n<li><strong>Instant Messaging<\/strong>: Lightweight real-time chat apps.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">When Not to Use It:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Stateful Applications<\/strong>: Where the previous state matters for the next transaction.<\/li>\n\n\n\n<li><strong>For Complex Queries<\/strong>: Like in databases where joins, updates, and deletes are frequently required.<\/li>\n\n\n\n<li><strong>Heavy Text-based Operations<\/strong>: MQTT isn&#8217;t designed for text-based protocols like JSON or XML parsing.<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\">Most Recommended Scenario:<\/h4>\n\n\n\n<p>The most recommended scenario for using MQTT is in IoT applications where bandwidth is constrained and you need real-time updates.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Code Example in C# and .NET Core<\/h3>\n\n\n\n<p>First, you&#8217;ll need to install the <code>M2Mqtt<\/code> package, a .NET library for MQTT.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;shell&quot;,&quot;mime&quot;:&quot;text\/x-sh&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;Shell&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;shell&quot;}\">dotnet add package M2Mqtt --version 4.3.0<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Publisher Example:<\/h4>\n\n\n\n<p>Here&#8217;s how you would publish a message on a topic.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">using System;\nusing uPLibrary.Networking.M2Mqtt;\nusing uPLibrary.Networking.M2Mqtt.Messages;\n\nclass Program {\n    static void Main(string[] args) {\n        MqttClient client = new MqttClient(&quot;broker_address&quot;);\n        string clientId = Guid.NewGuid().ToString();\n        client.Connect(clientId);\n        \n        string topic = &quot;home\/livingroom\/temperature&quot;;\n        string message = &quot;22.5&quot;;\n        \n        client.Publish(topic, System.Text.Encoding.UTF8.GetBytes(message), MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE, false);\n    }\n}\n<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Subscriber Example:<\/h4>\n\n\n\n<p>Here&#8217;s how you would subscribe to a topic.<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;showPanel&quot;:true,&quot;languageLabel&quot;:&quot;language&quot;,&quot;fullScreenButton&quot;:true,&quot;copyButton&quot;:true,&quot;mode&quot;:&quot;clike&quot;,&quot;mime&quot;:&quot;text\/x-csharp&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;fileName&quot;:&quot;&quot;,&quot;language&quot;:&quot;C#&quot;,&quot;maxHeight&quot;:&quot;400px&quot;,&quot;modeName&quot;:&quot;csharp&quot;}\">using System;\nusing uPLibrary.Networking.M2Mqtt;\nusing uPLibrary.Networking.M2Mqtt.Messages;\n\nclass Program {\n    static void Main(string[] args) {\n        MqttClient client = new MqttClient(&quot;broker_address&quot;);\n        \n        client.MqttMsgPublishReceived += Client_MqttMsgPublishReceived;\n        \n        string clientId = Guid.NewGuid().ToString();\n        client.Connect(clientId);\n        \n        client.Subscribe(new string[] { &quot;home\/livingroom\/temperature&quot; }, new byte[] { MqttMsgBase.QOS_LEVEL_EXACTLY_ONCE });\n    }\n\n    private static void Client_MqttMsgPublishReceived(object sender, MqttMsgPublishEventArgs e) {\n        string receivedMessage = System.Text.Encoding.UTF8.GetString(e.Message);\n        Console.WriteLine(&quot;Received message: &quot; + receivedMessage);\n    }\n}<\/pre><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Additional Important Information:<\/h4>\n\n\n\n<ul class=\"wp-block-list\">\n<li>MQTT brokers like Mosquitto, HiveMQ, or cloud-based options like AWS IoT Core and Azure IoT Hub are used for production systems.<\/li>\n\n\n\n<li>MQTT over Websockets: MQTT can also be run over websockets which makes it web-friendly.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>What is MQTT? MQTT stands for Message Queuing Telemetry Transport. It&#8217;s a lightweight publish\/subscribe messaging protocol designed for low-bandwidth, high-latency, or unreliable networks. MQTT has found its way into various kinds of systems like IoT devices, home automation, and even mobile apps. When and by whom it was developed: MQTT was originally developed by Andy [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":230,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[1],"tags":[45,36,37],"class_list":["post-41","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-blog","tag-about","tag-mqtt","tag-mqtt-architecture"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts\/41","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=41"}],"version-history":[{"count":1,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions"}],"predecessor-version":[{"id":42,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/posts\/41\/revisions\/42"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=\/wp\/v2\/media\/230"}],"wp:attachment":[{"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=41"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=41"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codeblog.xyz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=41"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}