MQTT - ECE-180D-WS-2023/Knowledge-Base-Wiki GitHub Wiki

Christian Ko & Anna Anderson

Table of contents

  1. What is MQTT?
  2. How does it work?

a. TCP/IP Explained

  1. Using MQTT
  2. Weaknesses of MQTT
  3. Conclusion

What is MQTT?

Message Queueing Telemetry Transport (MQTT) is a lightweight protocol used for connecting devices over limited bandwidth or unreliable networks. Because it is lightweight, open, and simple, it is often used for M2M (machine-to-machine) and IoT (Internet of Things) communication. MQTT runs on TCP/IP protocol, which is a communications standard protocol that enables programs and devices to exchange data as packets over a network, like the internet.

Andy Stanford-Clark and Arlen Nipper created MQTT in 1999, which was originally intended to control oil pipelines remotely via satellite. It was designed for minimal battery loss and bandwidth. MQTT was later standardized under OASIS, an organization that focuses on adoption of open standards.

We will be examining the foundation of the MQTT protocol by describing the basics of the TCP/IP protocol. We will give a game-based example on MQTT implementation later on in this tutorial. Finally, we will lay out the use cases of MQTT and discuss its notable weaknesses.

How does it work?

The MQTT architecture runs on a publish and subscribe (pub/sub) system. Unlike the traditional model in which a client directly communicates with a server, the pub/sub model separates the client that sends a message (the publisher) from other clients that receives the message (the subscriber). Instead, clients connect to a server, the broker, that handles receiving and sending communications. Publishers and subscribers are clients, which is any device that can use MQTT over TCP/IP, like a microcontroller or a computer.

Once connected to a broker, clients can publish messages. These messages have a payload, which is the data to transmit, and a topic that interested clients can subscribe to. Topics are strings that are hierarchically structured with the slash (/) character as delimiters, similar to folder structures in a computer file system. For example, “UCLA/HSSEAS/ECE/113” or “United States/California/Westwood”. Once a message is published and read, the broker processes it according to the Quality of Service (QoS) level indicated.

In order to receive messages, clients can subscribe to various topics, and once subscribed, can start receiving messages directed to those topics.

Figure 1
Figure 1. Example of an MQTT system

As Figure 1 shows, the smoke alarm is one client that publishes to the “SMOKE SENSOR” topic, which phone 1 and 2 are subscribed to, and the sprinklers are another client that subscribes to the “SPRINKLER SYSTEM” topic, which phone 2 publishes to.

TCP/IP Explained

Examining the TCP/IP protocol is an important component to fully understanding MQTT. In a networked system, there are many layers that work together to get data from one place to another (as seen in the graphic above) [1]. At a high level, the transport layer is responsible for maintaining an end-to-end connection between sender and receiver and ensuring the delivery of fragments of information called packets [2]. On the other hand, the network layer is responsible for addressing and interconnecting different networks making up the path on which data flows [1].

The Transmission Control Protocol, TCP, is a transport layer protocol used for breaking exchanged data into small packets, managing data “traffic” flow (congestion control), and ensuring that all packets arrive at the destination in the order that they were sent [1]. This reliability baseline provided by TCP, which is not possible through other transport methods like the User Datagram Protocol (UDP), gives MQTT the ability to allow users to customize the quality of service level for their specific needs. The Internet Protocol, IP, is a network layer protocol that utilizes hierarchical addressing to establish a path for data as it moves between local networks [3]. Routing algorithms are used to find the shortest path between two points in a network, and IP drops packets when they have visited too many nodes (tracked by Time To Live, TTL, information), signaling congestion to TCP [3]. These protocols make up the backbone of the modern internet and are harnessed by MQTT to facilitate communication.

Using MQTT

The general flow for creating an MQTT client and using it is as follows:

  1. Create a client instance
  2. Connect to a broker
  3. Subscribe on connect
  4. Loop to stay connected to the broker
  5. Publish as needed
  6. Disconnect from the broker at session end

This tutorial will use the Eclipse Paho MQTT Python client library.

A client instance can be constructed using the Client() function. If needed, you can set a last will message, which will be sent to the broker if the connection between the client and the broker is ever lost without calling the disconnect() function.

Functions connect() or connectasync() connect the client to the broker. connectasync() is used to delay the connection until loop_start() is called. The relevant argument these functions take is the hostname or IP address of the broker. Use disconnect() to end the connection to the broker cleanly, although any queued messages will not be sent if the connection ends.

Functions loop_start() and loop_stop() are used to process all incoming and outgoing network data.

Of course, in order to publish and subscribe, you would use publish(topic, payload, qos) and subscribe(topic, qos).

The topic parameter is the topic to publish or subscribe to.

The payload parameter is the message to be sent. It should be a string, but if an int or float is passed here, it will be converted to a string automatically. QoS is 0, 1, or 2, with 0 being the default value.

QoS Levels:

0 - The message will be sent once, without confirmation.

1 - The message will be sent at least once, so there is confirmation that the message was delivered to the broker or the subscriber.

2 - The message will be sent exactly once.

For most purposes in this class, a QoS (Quality of Service) level of 1, with code to handle duplicate messages is sufficient, as QoS level 0 can mean lost messages, while QoS level 2 takes too much time.

For games communications, most game state machines can be represented as follows:

Figure 2
Figure 2. MQTT Game Flow

Example in python3:

import paho.mqtt.client as mqtt

RECEIVED_DATA = False
DATA = ""

# 0. define callbacks - functions that run when events happen.
# The callback for when the client receives a CONNACK response from the server.
def on_connect(client, userdata, flags, rc):
    print("Connection returned result: " + str(rc))
    client.subscribe("kfc/chicken", qos=1)
    client.subscribe("kfc/soda", qos=1)

# The callback of the client when it disconnects.
def on_disconnect(client, userdata, rc):
    if rc != 0:
        print(’Unexpected Disconnect’)
    else:
        print(’Expected Disconnect’)

def on_message(client, userdata, message):
    if (message.topic == "kfc/chicken"):
	print("yummm " + str(message.payload))
	RECEIVED_DATA = True
	DATA = "message.payload"

# 1. create a client instance.
client = mqtt.Client()
client.on_connect = on_connect
client.on_disconnect = on_disconnect
client.on_message = on_message

# 2. connect to a broker using one of the connect*() functions.
client.connect_async(’mqtt.eclipseprojects.io’)

# 3. call one of the loop*() functions to maintain network traffic flow with the broker.
client.loop_start()

while True: # perhaps add a stopping condition using some break or something.

    # start game logic

        while (not RECEIVED_DATA):
	    # update game
	    pass

    # more game logic

    # reset
    RECEIVED_DATA = False
    DATA = ""

    break
    # this break can be elsewhere in this loop as a conditional

client.loop_stop()
client.disconnect()

Let's break this code block down a bit.

We first create variables that let the while loop run as long as communication has not been established and data has not been sent or received. In other words, once the MQTT client establishes a connection with the broker and starts the network loop, we initialize all game logic such as creating game objects, backgrounds, imports, etc.

Then as long as a message isn't published to the topic the client is subscribed to, we stay in the inner while loop where we can continuously run functions like updating the game background. At the end, we reset these data variables and either exit the loop or continue with the next iteration until a conditional stop is achieved.

Weaknesses of MQTT

While MQTT does have some notable strengths as described previously, it is important to acknowledge that it comes with some considerable limitations as well. These weaknesses need to be accounted for when using MQTT in a system. Otherwise, an alternative protocol should be used if they can not be overcome in the system of interest. Some alternatives to MQTT include HTTP and Nabto [4].

One major weakness of MQTT is that it cannot support video streaming [4]. There are no built-in utilities for streaming, so MQTT is not suitable for applications that require it [4]. This limitation is inherent from the TCP basis [2]. Generally because video/audio streaming requires little delay to maintain good quality, frames can be dropped during times of network congestion and this can often go unnoticed by users. This is not allowed by TCP, and so UDP protocols are often used instead for these purposes [2].

Another weakness is that MQTT has significant security vulnerabilities [4]. Security functionalities are not readily available, so they need to be implemented by the developer on top of MQTT if needed [4]. Many applications benefit from guaranteeing a secure connection, especially if critical data is being shared such as location information.

Conclusion

As a lightweight pub/sub messaging protocol, MQTT specializes in IOT applications in low bandwidth and unstable network environments. Unlike other protocols such as Hypertext Transfer Protocol (HTTP [The internet]), MQTT allows for communication in both directions between clients and servers, which serves well in our IOT game-making purposes.

Ultimately, when deciding on a messaging protocol for a design it is important to understand the technologies on which it relies, which we did here by taking a closer look at TCP/IP details. Additionally, one should also account for all strengths and weaknesses and look into alternative options to find the best suited protocol for the job.

Sources

[1] N. Security, “Network Layer,” Network layer. [Online]. Available: https://www.networxsecurity.org/members-area/glossary/n/network-layer.html. [Accessed: 24-Apr-2023].

[2] “Transport layer responsibilities,” GeeksforGeeks, 22-Apr-2023. [Online]. Available: https://www.geeksforgeeks.org/transport-layer-responsibilities/. [Accessed: 24-Apr-2023].

[3] “What is the internet protocol? | cloudflare.” [Online]. Available: https://www.cloudflare.com/learning/network-layer/internet-protocol/. [Accessed: 24-Apr-2023].

[4] I. Hübschmann, “The Pros and cons of using MQTT protocol in IOT,” Nabto, 22-Mar-2022. [Online]. Available: https://www.nabto.com/mqtt-protocol-iot/. [Accessed: 24-Apr-2023].

https://docs.oasis-open.org/mqtt/mqtt/v5.0/mqtt-v5.0.html

https://www.hivemq.com/blog/mqtt-essentials-part-1-introducing-mqtt/

https://ably.com/topic/mqtt

https://www.paessler.com/it-explained/mqtt

https://github.com/eclipse/paho.mqtt.python

https://www.integrasources.com/blog/mqtt-protocol-iot-devices/ (Figure 1)


Word Count: 1109