# MQTT Client

> Publish to MQTT Broker.

Source: https://docs.sommatic.ai/en/nodes/io.mqtt/

- Slug: `io.mqtt`
- Family: Input and output
- Version: 1.0.0
- Status: Partial. Always publishes (the declared `action` is ignored), with QoS 0 only.
- Execution: async

Publish to MQTT Broker.

## Configuration

- `connection_id` (string, required): Credential for the MQTT broker. Accepts credentials of type `basic-auth` (username + password + host + port), `connection-string` (mqtt:// / mqtts:// URI), or `object` (advanced MQTT config).
- `topic` (string, required)
- `message` (string, required): Message payload
- `retain` (boolean, optional): Default: `false`.
- `action` (string, optional): Default: `publish`. Allowed values: `publish`.

## Output ports

- `out` (default): Message successfully published
- `error`: Publish failed

## Input contract

- `connection_id` (string, required)
- `topic` (string, required)
- `message` (string, required)
- `retain` (boolean, optional)
- `action` (string, optional)

## Output contract

- `published` (boolean, optional): Publish success

## In expressions

What a later node can read from this node's output with `$("MQTT Client").item.json.<path>` (write your node's label instead of `MQTT Client`). **Editor** marks the paths the workflow editor suggests under Previous Nodes (the output contract); **Worker** marks the paths the worker really returns through `out`, from the output shown in Ports.

- `$("MQTT Client").item.json.published` (boolean): Publish success **The worker does not return it: reads as undefined.**
- `$("MQTT Client").item.json.result` (object) = `{…}` (Not suggested by the editor)
- `$("MQTT Client").item.json.result.published` (boolean) = `true` (Not suggested by the editor)
- `$("MQTT Client").item.json.result.topic` (string) = `"plant/line-2/commands"` (Not suggested by the editor)


## How it works

Connects to an MQTT broker, publishes one message to a topic and disconnects.

1. Resolves the config. `connection_id`, `topic`, `message` and `retain` (default false) fall back to the same keys of the incoming payload; the first three are required.
2. Resolves the credential: the broker URL is `config.url`, or `mqtt://<config.host>:<config.port>` (port default 1883); `username` and `password` come from the secret when present.
3. Connects, publishes `message` to `topic` with the `retain` flag, and closes the connection.
4. Returns `{ result: { published: true, topic } }` through `out`; any failure leaves through `error`.

## In a workflow

### Node

```json
{
  "node_id": "node-publish-command",
  "label": "Publish line command",
  "operator_slug": "io.mqtt",
  "operator_version": "1.0.0",
  "config": {
    "connection_id": "pi-credential-xxxx",
    "topic": "plant/line-2/commands",
    "message": "{{ JSON.stringify($json.command) }}",
    "retain": false
  }
}
```

### Envelope

```json
{
  "ok": true,
  "data": {
    "result": {
      "published": true,
      "topic": "plant/line-2/commands"
    }
  },
  "port": "out"
}
```

## Output ports (behaviour in the code)

### `out`

The broker accepted the publish.

```json
{
  "result": {
    "published": true,
    "topic": "plant/line-2/commands"
  }
}
```

### `error`

A required field is missing, the credential cannot be resolved, or the connection or publish failed.

```json
{
  "error": "connect ECONNREFUSED 203.0.113.10:1883"
}
```

## Behaviour in the code

- `connection_id`: The broker address comes from the credential config (`url`, or `host` + `port`); `username` and `password` from an object secret (`basic-auth`, `object`). A `connection-string` secret is not parsed: put the URI in the credential config `url`. A value starting with `mqtt://`, `mqtts://`, `ws://` or `wss://` is used as the broker URL itself, without credentials.
- `action`: Ignored: the node always publishes. Subscribing is not supported.
- `message`: Sent as the raw payload. To publish JSON, build the string first, for example `{{ JSON.stringify($json.command) }}`.

## Errors

| Message | Port | Cause and fix |
| --- | --- | --- |
| `connection_id is required` | `error` | No broker credential selected. |
| `topic is required` | `error` | The topic is empty in the config and in the incoming payload. |
| `message is required` | `error` | The message is empty. An empty string also fails: publish at least one character. |
| `Connection refused: Not authorized` | `error` | The broker rejected the user or password in the credential. |

## Notes and limits

- Publishes with the client's default QoS 0; no QoS option is exposed. A new connection is opened and closed on every run.
- The output contract declares `published` at the top level; the worker returns it inside `result`.


## Contracts (JSON Schema)

### All (`*.definition.js`)

```json
{
  "slug": "io.mqtt",
  "name": "MQTT Client",
  "category": "io",
  "version": "1.0.0",
  "description": "Publish to MQTT Broker.",
  "capabilities": [
    "mqtt",
    "iot",
    "messaging"
  ],
  "config_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string",
        "title": "MQTT Broker Credential",
        "description": "Credential for the MQTT broker. Accepts credentials of type `basic-auth` (username + password + host + port), `connection-string` (mqtt:// / mqtts:// URI), or `object` (advanced MQTT config).",
        "ui_widget": "connection_picker",
        "ui_options": {
          "credential_types": [
            "basic-auth",
            "connection-string",
            "object"
          ],
          "types": [
            "mqtt"
          ]
        }
      },
      "topic": {
        "type": "string"
      },
      "message": {
        "type": "string",
        "description": "Message payload"
      },
      "retain": {
        "type": "boolean",
        "default": false
      },
      "action": {
        "type": "string",
        "enum": [
          "publish"
        ],
        "default": "publish"
      }
    },
    "required": [
      "connection_id",
      "topic",
      "message"
    ]
  },
  "input_contract_schema": {
    "type": "object",
    "properties": {
      "connection_id": {
        "type": "string"
      },
      "topic": {
        "type": "string"
      },
      "message": {
        "type": "string"
      },
      "retain": {
        "type": "boolean"
      },
      "action": {
        "type": "string"
      }
    },
    "required": [
      "connection_id",
      "topic",
      "message"
    ]
  },
  "output_contract_schema": {
    "type": "object",
    "properties": {
      "published": {
        "type": "boolean",
        "description": "Publish success"
      }
    }
  },
  "ports": [
    {
      "port_id": "out",
      "label": "Published",
      "description": "Message successfully published",
      "is_default": true,
      "supports_condition": true
    },
    {
      "port_id": "error",
      "label": "Error",
      "description": "Publish failed",
      "is_default": false,
      "supports_condition": false
    }
  ],
  "execution_mode": "async",
  "required_resources": [],
  "compatible_worker_pools": [
    "standard-io",
    "iot"
  ],
  "tags": [
    "iot",
    "messaging"
  ],
  "is_stateful": false,
  "publisher": {
    "identity": "org_link_loom",
    "profile": {
      "name": "Link Loom",
      "verified": true,
      "url": "https://me.veripass.com/link-loom",
      "logo_url": null
    }
  }
}
```

### config_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string",
      "title": "MQTT Broker Credential",
      "description": "Credential for the MQTT broker. Accepts credentials of type `basic-auth` (username + password + host + port), `connection-string` (mqtt:// / mqtts:// URI), or `object` (advanced MQTT config).",
      "ui_widget": "connection_picker",
      "ui_options": {
        "credential_types": [
          "basic-auth",
          "connection-string",
          "object"
        ],
        "types": [
          "mqtt"
        ]
      }
    },
    "topic": {
      "type": "string"
    },
    "message": {
      "type": "string",
      "description": "Message payload"
    },
    "retain": {
      "type": "boolean",
      "default": false
    },
    "action": {
      "type": "string",
      "enum": [
        "publish"
      ],
      "default": "publish"
    }
  },
  "required": [
    "connection_id",
    "topic",
    "message"
  ]
}
```

### input_contract_schema

```json
{
  "type": "object",
  "properties": {
    "connection_id": {
      "type": "string"
    },
    "topic": {
      "type": "string"
    },
    "message": {
      "type": "string"
    },
    "retain": {
      "type": "boolean"
    },
    "action": {
      "type": "string"
    }
  },
  "required": [
    "connection_id",
    "topic",
    "message"
  ]
}
```

### output_contract_schema

```json
{
  "type": "object",
  "properties": {
    "published": {
      "type": "boolean",
      "description": "Publish success"
    }
  }
}
```

### ports

```json
[
  {
    "port_id": "out",
    "label": "Published",
    "description": "Message successfully published",
    "is_default": true,
    "supports_condition": true
  },
  {
    "port_id": "error",
    "label": "Error",
    "description": "Publish failed",
    "is_default": false,
    "supports_condition": false
  }
]
```
