MQTT publish-subscribe and unsubscribe
MQTT publish-subscribe pattern
The MQTT publish-subscribe pattern is something MQTT uses and implements. It is the foundational choice of communication for MQTT implements and this is in contrast to other popular protocols like HTTP who prefer the more traditional client-server protocol or a request-response protocol.
Usually, its works on a client and a server. The client is requesting data from the server and the server serves the data. so, for example, if you have a web browser and you want to browse Wikipedia your web browser would send a request to Wikipedia, and then you would get an answer with the data so the page back to your browser.
There are three participants at least in a published-subscribe pattern. you have a client who is publishing data, you have a broker who is the heart and the center of each communication and we have one or more subscribers who are subscribing to data.
The broker is really the core and is distributing data because we have a decoupling. so, the producing client which is called publisher, and MQTT sends a specific data point to the broker and the broker would then send it to all interested subscribers and these subscribers can also be publishers themselves because we have bi-directional communication.
Now, we will see in detail the most important operations MQTT has to offer publish-subscribe and unsubscribe.
In MQTT publish, as soon as a client has a connection established with the broker it can start producing messages. MQTT publishes the packet and sends it to the broker and this packet has mandatory information that a client must provide.
This is really for the protocol flows and this needs to be carried over the published packet. You can send up to 256 megabytes but usually, MQTT packets are below one kilobyte or one megabyte but you can also carry a lot of payloads here.
A topic is basically a string is text-based string with delimiters and this is used for message filtering. This must be provided because this is really the data it is sent to then.
Quality of service
This number indicates the Quality of Service (QoS) level of the message. There are three levels: 0,1 and 2. The service level determines the type of guarantee that a message has to reach the recipient broker.
This flag defines whether the message is saved by the broker as the last known good value for a specified subject. When a new customer subscribes to a topic, they receive the last retained message on this topic.
The payload field is responsible for carrying all the relevant information and this could be like an image file, this could be an XML file at JSON file.
So, what happens is the client sends the publish packet to the broker and the broker would find out who subscribed to it and then sends the same packet to multiple clients so we have this kind of amplification. So, a single message can be produced to multiple messages so multiple clients can receive the same message, and then the publish pack is acknowledged with a so-called puback packet, or if you have a quality of service flow we have more packets here.
So, if a client wants to receive data from a broker it sends a subscribe packet. A subscribe packet is a very simple packet, a client decides to send pairs based on a so-called topic filter and on the quality of service it wants to receive messages on.
A single subscribe packet can carry multiple of these pairs so you can carry multiple subscriptions into a single subscribe packet.
when we talk about MQTT we usually have thousands or even millions of topics which is very unique here.
and then we have the suback packet which comes back from the broker and the suback basically tells the client if the subscription was granted or not so based on other things like the permissions and so on.
The last important packet is the so-called unsubscribe packet and the unsubscribe package is used if you don’t want to receive a data stream anymore from your broker. You will send a packet with multiple topics to which you want to unsubscribe to and then the broker acknowledges the packet with an unsuback.
So, we have three important packet flows, we have the publish packet, subscribe packet, and the unsubscribe packet, and publishing is for producing data, subscribing is for receiving data streams and you receive a message, the message stream for these topics as soon as the broker sends you this suback and with unsubscribe, if you don’t want to participate in a message stream anymore, you can unsubscribe and the broker stops sending you messages again.