# MQTT

The MQTT protocol is based on TCP and is one of the most commonly used publish/subscribe protocols in use today. For a detailed overview of MQTT let's refer you to the Hivemq tutorial which has covered this subject pretty well.

{% embed url="<https://www.hivemq.com/blog/mqtt-essentials-part-1-introducing-mqtt/>" %}

What should you know about MQTT?

1. MQTT requires that all client connect to a broker. There are several public broker available today and you could launch your own MQTT broker as well.&#x20;
2. The clients of MQTT publish data on the topics which are hierarchical in nature. E.g. A topic could be formed for a university as follows:
   * /university/
   * /university/EECS/
   * /university/MECH/
3. MQTT protocol provides three quality of service(QoS) levels:&#x20;
   1. QoS 0: **at most once**
   2. QoS 1: **at least once**
   3. QoS 2: **exactly once**

### Overview of MQTT Template Settings

![](/files/EAUUMAXQkteimwsyoCWc)

In the protocol tab, you can modify several settings for the MQTT Protocol:

**MQTT Endpoint**: The name or IP address of the server along with the port number. It must be a public server IP address that is reachable over the internet.&#x20;

**Keep Alive**: The keepalive timer duration for the protocol. The hello message is sent every keepalive interval to make sure the server is aware of the client being connected.&#x20;

**QoS**: QoS settings ensure that the delivery of the message is guaranteed. Higher the QoS Settings more will be the latency of packet sending as the server will need to ensure that the messages are delivered with a guarantee.&#x20;

**Retain**: Whether the server should retain the messages for a future delivery.&#x20;

**Publication Topic**: The topic on which messages should be published. The topic field is scriptable, i.e. you could use template strings in **Topic** to dynamically change the content of the topic. For example,

```
/iotify/temperature/{{client()}}   // will translate to /iotify/temperature/0 
```

Any field within {{ }} will be evaluated at runtime and will be replaced with the actual contents.&#x20;

```
/iotify/temperature/{{state.topic}}    
```

Will change to whatever state.topic variable value is at the run time. For performance reasons, make sure that the fields are not too complex.&#x20;

**Subscription Topic**: (Can be enabled when required) The topic to which the client should subscribe. This field is scriptable but only at the beginning of the simulation (after the Init stage). For example, if you use {{state.topic}} within the Subscription Topic string, the subscription will be initialized to whatever value the topic had past the Init stage. However, the content will not change as the simulation runs.&#x20;

**Timeout**:  The timeout value to establish a connection with the server and wait for the messages to be sent. If the operation doesn't complete within the timeout period, it is marked as failed.&#x20;

**Security**: If the connection to the server requires authentication, the required fields can be passed here. For MQTT connection we support:

* Username and Password combination
* Pre-Shared Keys
* Certificates


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.iotify.io/concepts/protocol-settings/mqtt.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
