# MQTT Publish

You can invoke a UbiFunction by publishing data through MQTT. This makes it possible to integrate devices and gateways that may have a non-editable JSON payload that is not compatible with our [native MQTT broker](https://docs.ubidots.com/v1.6/reference/mqtt) or our [HTTP Data Ingestion API](https://docs.ubidots.com/v1.6/reference/http).

Because the UbiFunctions MQTT broker is shared across all functions, subscription requests are not supported **and only the MQTT Publish method is accepted**. Accordingly, all subscription requests are rejected.

{% hint style="success" %}
If you'd like to have a private MQTT broker mapped to a UbiFunction, [please reach out to our sales team](mailto:sales@ubidots.com). In this case, subscribe actions can be supported.
{% endhint %}

{% hint style="info" %}
The UbiFunction method must be set to `POST`.
{% endhint %}

Here are the MQTT settings needed to invoke a UbiFunction over MQTT:

| MQTT Settings | Value                                                   |
| ------------- | ------------------------------------------------------- |
| **URL**       | `functions.ubidots.com`                                 |
| **Port**      | <p>1883 for plain MQTT.<br>8883 for MQTT over TLS.</p>  |
| **Topic**     | `/prv/<your-username>/<function-label>`                 |
| **Username**  | Your account username                                   |
| **Password**  | Valid Token from your Ubidots account.                  |
| **ClientID**  | Any random string. Preferably longer than 15 characters |

### Topic structure

To trigger a UbiFunction through MQTT, the publish topic must follow this structure:

```
/prv/<username>/<function-label>
```

Where:

* `<username>` is your Ubidots account username.
* `<function-label>` is the function name in lowercase, with spaces replaced by dashes.

The base topic corresponds to the UbiFunction HTTPS Endpoint URL's path, as seen in the image below:

<figure><img src="/files/kkFxkFwhJGw6MjTzieae" alt=""><figcaption></figcaption></figure>

#### **Advanced Topics**

Additional topic levels are supported. To add more levels, simply add a forward slash ("`/`") after the base topic:

```
/prv/<username>/<function-label>/<level-1>/···/<level-N>
```

Where:

* `<level-N>` is any additional UTF-8 encoded string that you want to add to the topic.

Topics have the following limits:

| Topic Limits | Limit           |
| ------------ | --------------- |
| Length       | 1500 characters |
| Levels       | 10              |

### UbiFunction `args`

When triggering a UbiFunction through MQTT, you receive a JSON object in the `args` variable of the `main` function. The object has this structure:

```
{
  "topic": "<topic>",
  "payload": "<payload>",
  "from_client_id": "<client_id>",
}
```

Where:

| Key              | Type   | Value description                                          |
| ---------------- | ------ | ---------------------------------------------------------- |
| topic            | String | The multilevel publication topic                           |
| payload          | String | The payload sent by the device in the publication message. |
| from\_client\_id | String | The client ID used in the MQTT connection.                 |

### Certificates

Ubidots supports SSL V1.1, TLS V1.2, and V1.3. You can download our root certificates in different formats:

* [PEM file](https://z.cdn.ubidots.com/ustatic/certs/roots.pem): Certificate chain with two root certificates from our certificate authorities (CAs).
* [DER file](https://z.cdn.ubidots.com/ustatic/certs/roots.der): Same as the PEM file, with an alternative encoding.
* [CRT file](https://z.cdn.ubidots.com/ustatic/certs/roots.crt): Same as the PEM file, with a different extension. Often referred to as **.crt**, **.cert** or **.cer**.

### Examples

{% tabs %}
{% tab title="Port 1883" %}

```bash
mosquitto_pub \
-p 1883 \ 
-h functions.ubidots.com \
-t "/prv/<username>/<function-name>" \
-m '{"token": "TOKEN", "device": "device-label", "variable-label": value}' \
-u "<username>" \
-P "TOKEN" \
-q 1 -d
```

{% endtab %}

{% tab title="Port 8883" %}

```bash
mosquitto_pub \
-p 8883 \ 
-h functions.ubidots.com \
-t "/prv/<username>/<function-name>" \
-m '{"token": "TOKEN", "device": "device-label", "variable-label": value}' \
-u "<username>" \
-P "TOKEN" \
-q 1 -d \
--cafile /Users/user/Documents/Ubidots/roots.pem
```

{% endtab %}
{% endtabs %}


---

# 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://dev.ubidots.com/ubifunctions/invocation/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.
