# MQTT

> Publish messages to any MQTT broker, such as Mosquitto, HiveMQ, EMQX or AWS IoT Core, read a topic's latest message, and start a workflow when a sensor, machine or app sends a message to a topic.

App · 2 actions. Web page: https://docs.lodol.com/docs/integrations/mqtt

## Actions

### Get the latest message

Read the message the broker keeps as a topic's latest, such as a device's last reported state. Only messages published with Keep as the topic's latest message (retained) are kept; found is false when there is none.

**Inputs**

- Topic (`topic`, text, required): The topic, such as factory/line-1/status. Topics are paths separated by /, and capitals matter. It can't contain + or #.

**Outputs**: The topic's kept message, if the broker keeps one.

- `found` (true/false): Whether the broker keeps a message for the topic. When false, message is empty.
- `is_binary` (true/false): Whether the message isn't text, so message holds it as base64.
- `is_json` (true/false): Whether the message was JSON and has been read into fields.
- `message` (object): The message: its JSON read into fields when it is JSON, its text otherwise, or base64 when it isn't text (is_binary is then true).
- `size_bytes` (number): The message's size in bytes.
- `topic` (text): The topic read.

**Example output**

```json
{
  "found": true,
  "is_binary": false,
  "is_json": true,
  "message": {
    "state": "running",
    "temperature": 41.5
  },
  "size_bytes": 42,
  "topic": "factory/line-1/status"
}
```

### Publish a message

Send a message to a topic on the broker, for the devices and apps subscribed to it: a command to a machine, a reading, an alert. An object or list from an earlier step is sent as JSON.

**Inputs**

- Topic (`topic`, text, required): The topic, such as factory/line-1/status. Topics are paths separated by /, and capitals matter. It can't contain + or #.
- Message (`message`, text): What to send: text, or an object or list from an earlier step, which is sent as JSON. Leave it empty to send an empty message; with Keep as the topic's latest message on, that clears the message the broker keeps.
- Delivery (`delivery`, text): At least once waits for the broker to confirm it has the message, so the step fails if it doesn't arrive. At most once sends it without waiting, for frequent readings where one lost message doesn't matter. One of: `at_least_once` (At least once: the broker confirms it got the message), `at_most_once` (At most once: send it without waiting for the broker). Default: `at_least_once`.
- Keep as the topic's latest message (`keep_as_latest`, true/false): The broker keeps this message and hands it to anyone who subscribes to the topic later, such as a device's current state. Get the latest message reads it back. Each kept message replaces the one before. Default: `false`.

**Outputs**: The message that was published.

- `confirmed_by_broker` (true/false): Whether the broker confirmed it has the message: true for At least once.
- `delivery` (text): How it was delivered: at_least_once or at_most_once.
- `is_binary` (true/false): Whether the message isn't text, so message holds it as base64.
- `is_json` (true/false): Whether the message was JSON and has been read into fields.
- `kept_as_latest` (true/false): Whether the broker keeps it as the topic's latest message.
- `message` (object): The message: its JSON read into fields when it is JSON, its text otherwise, or base64 when it isn't text (is_binary is then true).
- `size_bytes` (number): The message's size in bytes.
- `topic` (text): The topic it was published to.

**Example output**

```json
{
  "confirmed_by_broker": true,
  "delivery": "at_least_once",
  "is_binary": false,
  "is_json": true,
  "kept_as_latest": false,
  "message": {
    "action": "start",
    "speed": 40
  },
  "size_bytes": 31,
  "topic": "factory/line-1/commands"
}
```
