<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: ZP X</title>
    <description>The latest articles on Forem by ZP X (@xizhibei).</description>
    <link>https://forem.com/xizhibei</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1225304%2Fdf78d397-eec2-41d1-87ad-728a24c207d8.png</url>
      <title>Forem: ZP X</title>
      <link>https://forem.com/xizhibei</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/xizhibei"/>
    <language>en</language>
    <item>
      <title>(MQTT Series) Part 3 - Publishing Subscribing and Topics</title>
      <dc:creator>ZP X</dc:creator>
      <pubDate>Fri, 10 May 2024 11:28:12 +0000</pubDate>
      <link>https://forem.com/xizhibei/mqtt-series-part-3-publishing-subscribing-and-topics-4c80</link>
      <guid>https://forem.com/xizhibei/mqtt-series-part-3-publishing-subscribing-and-topics-4c80</guid>
      <description>&lt;p&gt;Following up on the last introduction, let's discuss some basic concepts of MQTT.&lt;/p&gt;

&lt;h3&gt;
  
  
  Basic Concepts
&lt;/h3&gt;

&lt;p&gt;In the very simple MQTT Hello World last time, we actually touched on a very important concept: publishing and subscribing.&lt;/p&gt;

&lt;p&gt;It's easy to recall from design patterns, indeed, MQTT fundamentally implements an architectural publish-subscribe pattern.&lt;/p&gt;

&lt;p&gt;Let's recall, where's the benefit of the publish-subscribe pattern? Decoupling. If the observer pattern is a low coupling between sender and receiver, then the publish-subscribe pattern completely decouples them.&lt;/p&gt;

&lt;h3&gt;
  
  
  Difference from Message Queues
&lt;/h3&gt;

&lt;p&gt;Then what comes to mind are the various message queues in distributed applications (such as ActiveMQ, RabbitMQ, RocketMQ, Kafka, etc.), and it's easy to mistakenly think that they are similar, but their application scenarios and ranges are completely different.&lt;/p&gt;

&lt;p&gt;First, it's important to understand that MQTT is just an application layer protocol, comparable to the AMQP protocol in message queues, with MQTT Broker corresponding to various message queues.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Cloud message queue middleware communication protocols are more complex and do not need to consider complex network conditions, but MQTT is much simpler and requires less memory and network resources;&lt;/li&gt;
&lt;li&gt; Cloud message queue middleware communication protocols need to store messages, which will be stored indefinitely without client subscriptions, serving purposes like message buffering and smoothing peaks and valleys, whereas MQTT does not store messages, directly discarding them if there are no subscribers;&lt;/li&gt;
&lt;li&gt; MQTT clients will receive messages as long as they subscribe to a topic with data, but this is not necessarily the case with message queues, not only do the queues need to be created first, but in the case of multiple clients subscribing to the same queue, each message will be received by only one client;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;At this point, they can actually be used in combination, such as devices transmitting data to servers via the MQTT protocol, then placing it into message queues for caching to prevent data loss if the server cannot process timely.&lt;/p&gt;

&lt;p&gt;Speaking of which, actually, ActiveMQ &lt;a href="https://activemq.apache.org/mqtt"&gt;supports MQTT&lt;/a&gt;, and RabbitMQ also supports MQTT, see more details in &lt;a href="https://blog.rabbitmq.com/posts/2012/09/mqtt-adapter"&gt;MQTT Adapter&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Topic
&lt;/h3&gt;

&lt;p&gt;Topics in MQTT are easy to understand, you can think of them like paths in HTTP protocol or Linux, but you need to remove the first "root directory" because it represents an empty root directory in MQTT.&lt;/p&gt;

&lt;p&gt;You can send any data to any topic if you have the permission, and you can also subscribe, but note three symbols:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; '+' represents a single-level directory match, it can only be placed between directories, not combined with other characters;

&lt;ol&gt;
&lt;li&gt; Valid examples:

&lt;ol&gt;
&lt;li&gt; a/b/c/+&lt;/li&gt;
&lt;li&gt; a/+/c&lt;/li&gt;
&lt;li&gt; a/+/c/+/e&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt; Invalid examples:

&lt;ol&gt;
&lt;li&gt; a/b/c+&lt;/li&gt;
&lt;li&gt; a+&lt;/li&gt;
&lt;li&gt; a/+b&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt; '#' represents a multi-level directory match, it can only be the last part of a subscription topic, if there is content before it, it must have a '/', you can also think of it as subscribing to all topics with its preceding content as a prefix;

&lt;ol&gt;
&lt;li&gt; Valid examples:

&lt;ol&gt;
&lt;li&gt; # &lt;/li&gt;
&lt;li&gt; a/#&lt;/li&gt;
&lt;li&gt; a/b/c/#&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;li&gt; Invalid examples:

&lt;ol&gt;
&lt;li&gt; a#&lt;/li&gt;
&lt;li&gt; #a&lt;/li&gt;
&lt;li&gt; #/a/b&lt;/li&gt;
&lt;/ol&gt;


&lt;/li&gt;
&lt;/ol&gt;
&lt;/li&gt;
&lt;li&gt; '$' is a reserved prefix for internal topics, even if you subscribe with a single '#', the Broker will not send them to you unless you explicitly subscribe, like the common &lt;a href="https://github.com/mqtt/mqtt.org/wiki/SYS-Topics"&gt;&lt;code&gt;$SYS topics&lt;/code&gt;&lt;/a&gt;;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Additionally, aside from testing, try not to subscribe to the '#' topic, as it's likely to cause problems when the client sends too much data.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example
&lt;/h3&gt;

&lt;p&gt;Before continuing, it's best to set up your own local test Broker to avoid interference from other people's messages on public servers.&lt;/p&gt;

&lt;p&gt;Below, we'll use Go as an example to demonstrate message publishing and receiving.&lt;/p&gt;

&lt;p&gt;The most commonly used library currently is &lt;a href="https://github.com/eclipse/paho.mqtt.golang"&gt;paho.mqtt.golang&lt;/a&gt;, which can be obtained directly by using:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;go get github.com/eclipse/paho.mqtt.golang
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As an MQTT client, the first thing to do is establish a connection.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;opts&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;mqtt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewClientOptions&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
  &lt;span class="n"&gt;AddBroker&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"tcp://localhost:1883"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;
  &lt;span class="n"&gt;SetClientID&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"test-client-id"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;mqtt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewClient&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;opts&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Connect&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Disconnect&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;250&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sleep&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;time&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Second&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the example above, we established a connection with the simplest options and disconnected after a second. If you're interested in the options here, you can see &lt;a href="https://github.com/eclipse/paho.mqtt.golang/blob/04f56444eae54291f9194f479bb4185b4d7f17ed/options.go?_pjax=%23js-repo-pjax-container%2C%20div%5Bitemtype%3D%22http%3A%2F%2Fschema.org%2FSoftwareSourceCode%22%5D%20main%2C%20%5Bdata-pjax-container%5D#L101"&gt;MQTT Client options&lt;/a&gt;, where the default options are clear at a glance.&lt;/p&gt;

&lt;p&gt;Next is publishing and subscribing, below is a very simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Subscribe&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testtopic/#"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;func&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="n"&gt;mqtt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Client&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;m&lt;/span&gt; &lt;span class="n"&gt;mqtt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;m&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Payload&lt;/span&gt;&lt;span class="p"&gt;()))&lt;/span&gt;
    &lt;span class="p"&gt;})&lt;/span&gt;
    &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
       &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Println&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;
       &lt;span class="n"&gt;os&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Exit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;token&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Publish&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"testtopic/123"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;false&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"Hello world"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;time.Sleep(10 * time.Second)&lt;/p&gt;

&lt;p&gt;Alternatively, you can also try linking publishing and subscribing as mentioned in the first article, such as sending data on the program and receiving on the desktop client, and vice versa.&lt;/p&gt;

&lt;h3&gt;
  
  
  Finally
&lt;/h3&gt;

&lt;p&gt;In this introductory article, we omitted connection parameters, as well as &lt;code&gt;QoS&lt;/code&gt; and &lt;code&gt;Retained&lt;/code&gt; two parameters during publishing and subscribing, which are very important details. They will appear in future articles (rest assured, we will let your descendants notify you of updates 🙈).&lt;/p&gt;

</description>
      <category>mqtt</category>
    </item>
    <item>
      <title>(MQTT Series) Part 2 - Setting Up a Broker</title>
      <dc:creator>ZP X</dc:creator>
      <pubDate>Fri, 10 May 2024 11:24:20 +0000</pubDate>
      <link>https://forem.com/xizhibei/mqtt-series-part-2-setting-up-a-broker-4ed8</link>
      <guid>https://forem.com/xizhibei/mqtt-series-part-2-setting-up-a-broker-4ed8</guid>
      <description>&lt;p&gt;In my last &lt;a href="https://blog.xizhibei.me/en/2021/08/29/mqtt-1-intro-hello-world/"&gt;introduction&lt;/a&gt;, I briefly mentioned how to use a public Broker for testing. Obviously, you can't use a test server as a production environment server; you need one of your own.&lt;/p&gt;

&lt;h3&gt;
  
  
  Mosquitto
&lt;/h3&gt;

&lt;p&gt;Mosquitto is arguably the most famous open-source MQTT Broker, with just enough functionality. Some advanced features like permission management require the installation of plugins, or even custom plugin development to extend its capabilities.&lt;/p&gt;

&lt;p&gt;It also offers a public Broker for testing: &lt;a href="https://test.mosquitto.org/"&gt;https://test.mosquitto.org/&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Installing it is very straightforward, just install the appropriate package, for example, on Mac &lt;code&gt;brew install mosquitto&lt;/code&gt;, and on Linux &lt;code&gt;sudo api install mosquitto&lt;/code&gt;. If you prefer Docker, the official image is &lt;code&gt;eclipse-mosquitto&lt;/code&gt;. I'll skip the running details and focus mainly on its configuration&lt;sup&gt;&lt;a href="https://mosquitto.org/man/mosquitto-conf-5.html"&gt;1&lt;/a&gt;&lt;/sup&gt;:&lt;/p&gt;

&lt;p&gt;Listening on the default unencrypted port 1883:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    listener 1883 0.0.0.0
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If you don't want to configure user password login, here you can configure to allow anonymous connections, meaning no user password:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    allow_anonymous true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if you configured to disallow anonymous access, then you need to set up username and password. The user password in this file can be configured using the tool provided by mosquitto: &lt;code&gt;mosquitto_passwd mosquitto/config/pwfile username&lt;/code&gt;, and then follow the prompt to enter the password.&lt;/p&gt;

&lt;p&gt;Additionally, you need to add this line in the configuration file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    password_file /mosquitto/config/pwfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Furthermore, if you need to restrict permissions for each user, you need to configure an ACL:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    acl_file /mosquitto/config/aclfile
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This configuration is simple, it supports three syntaxes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; &lt;code&gt;topic [read|write|readwrite|deny] &amp;lt;topic&amp;gt;&lt;/code&gt;, this can set permissions for anonymous client topics;&lt;/li&gt;
&lt;li&gt; &lt;code&gt;user &amp;lt;username&amp;gt;&lt;/code&gt;, this is used in conjunction with topic permissions;&lt;/li&gt;
&lt;li&gt; &lt;code&gt;pattern [read|write|readwrite] &amp;lt;topic&amp;gt;&lt;/code&gt;, this can be used for individual user permissions, where &lt;code&gt;&amp;lt;topic&amp;gt;&lt;/code&gt; can contain &lt;code&gt;%c&lt;/code&gt; representing the logged-in Client ID and &lt;code&gt;%u&lt;/code&gt; representing the username;&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Here's an example:&lt;sup&gt;&lt;a href="https://troy.dack.com.au/mosquitto-mqtt/"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;Allow anonymous users to read all user-level topics:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    topic read #
    topic read $SYS/broker/messages/#
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Allow user 'web' to read all topics:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    user web
    topic read #
    topic read $SYS/#
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Clearly, this level of permissions only satisfies the most basic requirements. If you need to integrate with your platform to implement dynamic login authentication, you would need to use an auth_plugin. One officially recommended plugin is &lt;a href="https://github.com/iegomez/mosquitto-go-auth"&gt;mosquitto-go-auth&lt;/a&gt;.&lt;/p&gt;

&lt;h4&gt;
  
  
  Clustering
&lt;/h4&gt;

&lt;p&gt;Mosquitto itself does not support cluster deployment, but it can be implemented through the backend, see &lt;a href="https://github.com/mqtt/mqtt.org/wiki/server-support"&gt;MQTT server support&lt;/a&gt; for details.&lt;/p&gt;

&lt;h4&gt;
  
  
  TLS Certificates
&lt;/h4&gt;

&lt;p&gt;With increasing national requirements for privacy protection, encrypted transmission is becoming an increasingly important component, meaning all personal information transmission must be encrypted.&lt;/p&gt;

&lt;p&gt;For MQTT, HTTPS certificates can be used because fundamentally, they are both TLS certificates and thus can be applied to MQTT as well.&lt;/p&gt;

&lt;p&gt;If you use a certificate issued and signed by an authoritative CA, simple configuration would be:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    listener 8883 0.0.0.0
    certfile /path/to/certs/example.com.cer
    keyfile /path/to/certs/example.com.key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But if using a self-signed certificate, the client connection process is a bit more complex, requiring proper CA configuration.&lt;/p&gt;

&lt;p&gt;Like &lt;a href="https://github.com/xizhibei/blog/issues/159"&gt;HTTPS mutual authentication&lt;/a&gt;, MQTT can also use mutual authentication. In this case, when a client connects, the server will require the client to provide a certificate and use your configured CA certificate to verify the client certificate's signature.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    cafile /path/to/certs/ca.pem
    require_certificate true
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h4&gt;
  
  
  Testing
&lt;/h4&gt;

&lt;p&gt;Once setup is complete, you can perform simple tests using a client. However, after a basic test, most people might think it's ready for full use, but you can do more to ensure reliability.&lt;/p&gt;

&lt;p&gt;For instance, you might estimate the number of client connections you need, the number of messages, concurrency, and message sizes to get a general range, and then perform benchmark testing.&lt;/p&gt;

&lt;p&gt;I used the &lt;a href="https://github.com/krylovsk/mqtt-benchmark"&gt;MQTT benchmarking tool&lt;/a&gt;, which easily tests the stress your newly setup Broker can handle.&lt;/p&gt;

&lt;p&gt;It's fairly user-friendly; if you've used HTTP benchmarking tools like Apache Bench, you'll quickly get the hang of it. For example, from its homepage, a typical scenario is 10 clients, each sending 100 consecutive messages:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;mqtt-benchmark &lt;span class="nt"&gt;--broker&lt;/span&gt; tcp://broker.local:1883 &lt;span class="nt"&gt;--count&lt;/span&gt; 100 &lt;span class="nt"&gt;--clients&lt;/span&gt; 10 &lt;span class="nt"&gt;--qos&lt;/span&gt; 1 &lt;span class="nt"&gt;--topic&lt;/span&gt; house/bedroom/temperature &lt;span class="nt"&gt;--payload&lt;/span&gt; &lt;span class="o"&gt;{&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;temperature&lt;span class="se"&gt;\"&lt;/span&gt;:20,&lt;span class="se"&gt;\"&lt;/span&gt;timestamp&lt;span class="se"&gt;\"&lt;/span&gt;:1597314150&lt;span class="o"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the output, you'll see the results of the test and can identify potential issues that were not apparent during setup. Although spending an extra hour or two might seem wasteful, discovering these issues after some usage would cost much more than these additional hours. Plus, I believe the difference between engineers isn't just in speed but in such professional diligence.&lt;/p&gt;

&lt;h3&gt;
  
  
  P.S.
&lt;/h3&gt;

&lt;p&gt;You could also consider using a paid service to avoid maintenance labor and server costs. For instance, you could choose commercial Brokers like China's EMQX and international HiveMQ. They support both commercial and open-source versions, and you can either set up on your own servers or use their provided servers. Being commercially supported, they offer more robust features and generally a better experience.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ref
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;a href="https://mosquitto.org/man/mosquitto-conf-5.html"&gt;mosquitto-conf&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; &lt;a href="https://troy.dack.com.au/mosquitto-mqtt/"&gt;mosquitto-mqtt&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Original published at &lt;a href="https://blog.xizhibei.me/en/2021/10/31/mqtt-2-mosquitto-broker-setup/"&gt;https://blog.xizhibei.me/en/2021/10/31/mqtt-2-mosquitto-broker-setup/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mqtt</category>
    </item>
    <item>
      <title>(MQTT Series) Part 1 - Introduction: Hello World</title>
      <dc:creator>ZP X</dc:creator>
      <pubDate>Fri, 10 May 2024 11:19:01 +0000</pubDate>
      <link>https://forem.com/xizhibei/mqtt-series-part-1-introduction-hello-world-1i27</link>
      <guid>https://forem.com/xizhibei/mqtt-series-part-1-introduction-hello-world-1i27</guid>
      <description>&lt;h3&gt;
  
  
  Introduction to MQTT
&lt;/h3&gt;

&lt;p&gt;MQTT is a very simple protocol, originally designed in 1999 by two IBM engineers, Andy Stanford-Clark and Arlen Nipper, for monitoring oil pipelines. It was designed for scenarios with limited bandwidth, lightweight, and very low power consumption. At that time, satellite bandwidth was just so small and painfully expensive.&lt;sup&gt;&lt;a href="https://en.wikipedia.org/wiki/MQTT" rel="noopener noreferrer"&gt;1&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;In modern society, although the cost of bandwidth has greatly decreased, there are still many scenarios where this protocol is needed, such as in smart homes (still part of IoT). Many small IoT devices rely on a button cell battery to function for years, making MQTT very suitable as an application layer transmission protocol.&lt;/p&gt;

&lt;p&gt;In summary, MQTT is a client-server architecture publish-subscribe messaging transmission protocol. It is very lightweight, open, and simple, making it very easy to implement. These characteristics make it highly suitable for fields like machine-to-machine (M2M) and Internet of Things (IoT), which are limited by small memory and narrow bandwidth.&lt;sup&gt;&lt;a href="http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html" rel="noopener noreferrer"&gt;2&lt;/a&gt;&lt;/sup&gt;&lt;/p&gt;

&lt;p&gt;IBM submitted version 3.1 to OASIS in 2013, and in 2014, OASIS made minor changes and released version 3.1.1.&lt;/p&gt;

&lt;p&gt;In 2019, OASIS added many features to MQTT, such as better error handling, shared subscriptions, message content types, etc., and upgraded to version 5. These features will be discussed in dedicated chapters later on.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hello World
&lt;/h3&gt;

&lt;p&gt;First, you need an MQTT Broker. Install mosquitto … oh? You don't know what that is? Okay, let's try a simpler approach.&lt;/p&gt;

&lt;p&gt;First, we can use some public ones, like China's EMQ (Hangzhou Yingyun Technology Co., Ltd.) provides broker.emqx.io (I must advertise for domestic software here, their MQTTX client is the most user-friendly I've used so far, and the Broker's features are also very powerful, I plan to specifically introduce server setup in a separate article).&lt;/p&gt;

&lt;p&gt;Then, their &lt;a href="https://mqttx.app/" rel="noopener noreferrer"&gt;MQTTX client&lt;/a&gt;, implemented in Electron, supports all platforms, just download and use.&lt;/p&gt;

&lt;p&gt;Open the MQTTX client, let's start a simple test.&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt; Click the + on the left sidebar to create a connection (this +, I think does not conform to interaction logic, as a creation button it should be a different level from other buttons, better placed together with new group creation);&lt;/li&gt;
&lt;li&gt; A creation page will then pop up, fill in a name randomly, and click connect. If there are no network issues, you should be able to connect successfully (see, they know you're lazy, all the details like Broker address and port are filled in for you, which is also very valuable for us making tech products, on how to let users start using the product with the lowest cost);&lt;/li&gt;
&lt;li&gt; Now, let's create a subscription, click add subscription on the page, in the popup dialog, fill in a somewhat random topic like &lt;code&gt;test/907839342134&lt;/code&gt; to avoid conflicts with others, as this is a public Broker, then click confirm.&lt;/li&gt;
&lt;li&gt; Finally, let's publish a message. In the bottom left corner, there's an input box prompting you to enter a Topic, we enter &lt;code&gt;test/907839342134&lt;/code&gt;, and in the content box below it, enter &lt;code&gt;{"hello": "world"}&lt;/code&gt;, click the paper airplane below, and after sending, you will see that you have received the message you sent to yourself.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.xizhibei.me%2Fmedia%2F16253875626915%2F16302244840845.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fblog.xizhibei.me%2Fmedia%2F16253875626915%2F16302244840845.jpg" alt="mqttx"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;That's it for now, a very simple introduction. In the next installments, I will introduce MQTT concepts, principles, and practical applications in more detail.&lt;/p&gt;

&lt;h3&gt;
  
  
  Ref
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt; &lt;a href="https://en.wikipedia.org/wiki/MQTT" rel="noopener noreferrer"&gt;MQTT&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt; &lt;a href="http://docs.oasis-open.org/mqtt/mqtt/v3.1.1/mqtt-v3.1.1.html" rel="noopener noreferrer"&gt;MQTT Version 3.1.1 Plus Errata 01&lt;/a&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Original published at &lt;a href="https://blog.xizhibei.me/en/2021/08/29/mqtt-1-intro-hello-world/" rel="noopener noreferrer"&gt;https://blog.xizhibei.me/en/2021/08/29/mqtt-1-intro-hello-world/&lt;/a&gt;&lt;/p&gt;

</description>
      <category>mqtt</category>
    </item>
  </channel>
</rss>
