{"id":3047,"date":"2016-07-04T12:33:39","date_gmt":"2016-07-04T18:33:39","guid":{"rendered":"http:\/\/dev.iachieved.it\/iachievedit\/?p=3047"},"modified":"2016-07-04T12:33:39","modified_gmt":"2016-07-04T18:33:39","slug":"mqtt-last-will-and-testament","status":"publish","type":"post","link":"https:\/\/dev.iachieved.it\/iachievedit\/mqtt-last-will-and-testament\/","title":{"rendered":"MQTT Last Will and Testament"},"content":{"rendered":"<p>This is another post in a series on writing <a href=\"https:\/\/en.wikipedia.org\/wiki\/MQTT\">MQTT<\/a> clients using Swift on Linux.<\/p>\n<p>In this post we&#8217;ll look at the MQTT <a href=\"http:\/\/www.hivemq.com\/blog\/mqtt-essentials-part-9-last-will-and-testament\">Last Will and Testament<\/a> message.  Essentially the LWT message is predefined by a client connecting to the broker.  In the event the client <i>abnormally<\/i> disconnects, the broker will then broadcast the LWT to any subscribers of the LWT message topic.<\/p>\n<p>For example, let&#8217;s say you are building a chat application where each client connects to a broker and then subscribes to the topic <code>\/chat\/hottub<\/code>.  Messages then published to <code>\/chat\/hottub<\/code> are received by all of the subscribers.  A simple but effective way to join a &#8220;chat room&#8221; (i.e., MQTT topic).<\/p>\n<p>When a client leaves the chat under normal conditions we might expect a message like &#8220;Joe has left \/chat\/hottub.&#8221;  This would be easy to do; when the user types <code>\/exit<\/code> then publish an appropriate message and then terminate the client.  If a client <i>abnormally<\/i> disconnects (loss of network, client crash, etc.), then what was given to the broker as the Last Will and Testament message is used instead.<\/p>\n<p>Here&#8217;s how we set up the Last Will message for our MQTT client:<\/p>\n<pre class=\"lang:swift\">\nclass Client:MQTT, MQTTDelegate {\n  init(clientId:String) {\n    super.init(clientId:clientId)\n    super.willMessage =  MQTTWill(topic:\"\/chat\/hottub\",\n                                  message:\"{\\\"client\\\":\\\"\\(clientId)\\\",\\\"message\\\":\\\"Abnormal Disconnect\\\"}\")\n    super.delegate = self\n  }\n  ...\n}\n<\/pre>\n<p><code>willMessage<\/code> is a <code>MQTTWill<\/code> property of the <code>MQTT<\/code> class.  <code>MQTTWill<\/code> is constructed with a topic and message.  In our example here the topic will be our chat channel <code>\/chat\/hottub<\/code>, and the message will be a JSON string that contains our client&#8217;s ID and a simple <code>Abnormal Disconnect<\/code> string.<\/p>\n<h3>Get the Code<\/h3>\n<p>We&#8217;ve been building Swift 3.0 directly from the latest in the Apple repositories and continually upgrade our code.  To use these examples you&#8217;ll need to install Swift 3.0 from our <a href=\"http:\/\/dev.iachieved.it\/iachievedit\/introducing-swift-3-0\/\">apt-get repository<\/a>.<\/p>\n<p>An example of our MQTT HotTub is in <a href=\"https:\/\/github.com\/iachievedit\/MQTTHotTub\">GitHub<\/a>.<\/p>\n<pre class=\"crayon:false\">\n# git clone https:\/\/github.com\/iachievedit\/MQTTHotTub\n# cd MQTTHotTub\n# swift build\n<\/pre>\n<p>To test things out you&#8217;ll want to run the <code>MQTTHotTub<\/code> twice, so open a second terminal.<\/p>\n<p><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/1__clear_____build_debug_MQTTHotTub__ssh__and_9__clear_____build_debug_MQTTHotTub__ssh_-1.png\" rel=\"attachment wp-att-3157\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/1__clear_____build_debug_MQTTHotTub__ssh__and_9__clear_____build_debug_MQTTHotTub__ssh_-1.png\" alt=\"1__clear_____build_debug_MQTTHotTub__ssh__and_9__clear_____build_debug_MQTTHotTub__ssh_\" width=\"925\" height=\"479\" class=\"aligncenter size-full wp-image-3157\" srcset=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/1__clear_____build_debug_MQTTHotTub__ssh__and_9__clear_____build_debug_MQTTHotTub__ssh_-1.png 925w, https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/1__clear_____build_debug_MQTTHotTub__ssh__and_9__clear_____build_debug_MQTTHotTub__ssh_-1-300x155.png 300w, https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/1__clear_____build_debug_MQTTHotTub__ssh__and_9__clear_____build_debug_MQTTHotTub__ssh_-1-768x398.png 768w\" sizes=\"(max-width: 925px) 100vw, 925px\" \/><\/a><\/p>\n<p>Run the clients with <code>.build\/debug\/MQTTHotTub<\/code>.<\/p>\n<h3>Prisencolinensinainciusol<\/h3>\n<p>Our <code>MQTTHotTub<\/code> client simulates a chat of <a href=\"https:\/\/www.youtube.com\/watch?v=Kj5TL1l9QYQ&#038;t=1m37s\">gibberish<\/a>.  Each published message is delivered as JSON:<\/p>\n<pre class=\"crayon:false\">\n{\"client\":\"octxktfo\", \"message\":\"Gyxswhz nsoxfnj gz.\"}\n{\"client\":\"ajyhyjic\", \"message\":\"Cmr w bzwubzv mwfhtklz.\"}\n<\/pre>\n<p>When a client receives a message that it did not send it gets filtered out:<\/p>\n<pre class=\"lang:swift\">\nif cid != clientId {\n  SLogInfo(\"Received \\\"\\(msg)\\\" from \\(cid)\")\n}\n<\/pre>\n<p>Remember, if we are listening to messages published to a given topic, and we publish to that topic, the client will also receive that message back as an echo.  This is the purpose of filtering with <code>cid != clientId<\/code>.<\/p>\n<p>You can see each client logging what they are receiving from other clients:<\/p>\n<pre class=\"crayon:false\">\nReceived \"Wlfu zrqyj tady obxnjl lupihobi nph oapplt nyidmja.\" from octxktfo\nReceived \"Cmr w bzwubzv mwfhtklz.\" from ajyhyjic\n<\/pre>\n<p>Now, CTRL-C one of the clients, and notice what is received by the remaining client(s):<\/p>\n<pre class=\"crayon:false\">\nReceived \"Abnormal Disconnect\" from octxktfo\n<\/pre>\n<figure id=\"attachment_3163\" aria-describedby=\"caption-attachment-3163\" style=\"width: 921px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/abnormalDisconnect.png\" rel=\"attachment wp-att-3163\"><img loading=\"lazy\" decoding=\"async\" src=\"http:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/abnormalDisconnect.png\" alt=\"Abnormal Disconnect\" width=\"921\" height=\"475\" class=\"size-full wp-image-3163\" srcset=\"https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/abnormalDisconnect.png 921w, https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/abnormalDisconnect-300x155.png 300w, https:\/\/dev.iachieved.it\/iachievedit\/wp-content\/uploads\/2016\/07\/abnormalDisconnect-768x396.png 768w\" sizes=\"(max-width: 921px) 100vw, 921px\" \/><\/a><figcaption id=\"caption-attachment-3163\" class=\"wp-caption-text\">Abnormal Disconnect<\/figcaption><\/figure>\n<p>This is the MQTT Last Will and Testament message in action; the aborted client had no opportunity to broadcast that it was unavailable so instead the broker sent out the client&#8217;s Last Will on the <code>\/chat\/hottub<\/code> topic:<\/p>\n<pre class=\"crayon:false\">\n{\"client\":\"\\(clientId)\",\"message\":\"Abnormal Disconnect\"}\n<\/pre>\n<h3>Do I Need to Have a Last Will?<\/h3>\n<p>Need is a strong word, and the answer is <i>no<\/i>, your MQTT client does not <i>need<\/i> to supply a Last Will message.  There are plenty of <a href=\"http:\/\/stackoverflow.com\/a\/17385293\/843150\">additional examples<\/a> that provide guidance on when you might want to use one.<\/p>\n<h2>What&#8217;s Next?<\/h2>\n<p>We&#8217;re continuing to work hard on our <a href=\"https:\/\/github.com\/iachievedit\/mqtt\">MQTT<\/a> implementation for Swift on Linux.  With Last Will and Testament now working our attention will turn to implementing secure MQTT connections (MQTT SSL).  Stay tuned!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This is another post in a series on writing MQTT clients using Swift on Linux. In this post we&#8217;ll look at the MQTT Last Will and Testament message. Essentially the LWT message is predefined by a client connecting to the broker. In the event the client abnormally disconnects, the broker will then broadcast the LWT [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":3161,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,5],"tags":[27,26,56,7,55],"class_list":["post-3047","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-linux","category-swift","tag-linux","tag-mqtt","tag-mqtt-last-will","tag-swift-2","tag-swift-mqtt-client"],"_links":{"self":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3047"}],"collection":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/comments?post=3047"}],"version-history":[{"count":9,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3047\/revisions"}],"predecessor-version":[{"id":3165,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/posts\/3047\/revisions\/3165"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media\/3161"}],"wp:attachment":[{"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/media?parent=3047"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/categories?post=3047"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dev.iachieved.it\/iachievedit\/wp-json\/wp\/v2\/tags?post=3047"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}