本页内容
Sommatic 文档
打开控制台

MQTT Client

向 MQTT Broker 发布消息。

以 Markdown 查看
在以下应用中打开
输入与输出 部分可用 io.mqtt 版本 1.0.0 执行方式: async

连接 MQTT broker,向一个主题发布一条消息后断开连接。

工作原理

  1. 解析配置。connection_id、topic、message 和 retain(默认 false)缺失时回退到输入 payload 中的同名键;前三项为必填。
  2. 解析凭据:broker URL 为 config.url,或 mqtt://<config.host>:<config.port>(端口默认 1883);密钥中存在 username 和 password 时使用它们。
  3. 建立连接,带 retain 标志向 topic 发布 message,然后关闭连接。
  4. 从 out 返回 { result: { published: true, topic } };任何失败都从 error 输出。

在工作流中

节点在工作流文件(draft_nodes[])中的写法,以及一次成功运行的 envelope。

{
  "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
  }
}

配置

connection_id string 必填

MQTT Broker 的凭据。接受 `basic-auth`(用户名 + 密码 + 主机 + 端口)、`connection-string`(mqtt:// / mqtts:// URI)或 `object`(高级 MQTT 配置)类型的凭据。

topic string 必填
message string 必填

消息载荷

retain boolean 可选

默认值: false

action string 可选

默认值: publish

可选值: publish

代码中的行为

除 schema 描述之外,实现代码对这些键的实际处理。

connection_id
broker 地址取自凭据配置(url,或 host + port);username 和 password 取自对象类型密钥(basic-auth、object)。connection-string 类型的密钥不会被解析:请将 URI 写入凭据配置的 url。以 mqtt://、mqtts://、ws:// 或 wss:// 开头的值会直接用作 broker URL,不带凭据。
action
被忽略:节点始终执行发布,不支持订阅。
message
作为原始 payload 发送。如需发布 JSON,请先构造字符串,例如 {{ JSON.stringify($json.command) }}。

输出端口

out 默认

broker 已接受发布。

数据
{
  "result": {
    "published": true,
    "topic": "plant/line-2/commands"
  }
}
error

缺少必填字段、无法解析凭据,或连接/发布失败。

数据
{
  "error": "connect ECONNREFUSED 203.0.113.10:1883"
}

在表达式中

后续节点可通过 $("MQTT Client").item.json.<路径> 读取此节点输出中的内容(请把 MQTT Client 换成你的节点标签)。Editor 标记工作流编辑器在 Previous Nodes 中提示的路径(输出契约);Worker 标记 worker 实际通过 out 返回的路径,依据“端口”一节所示的输出。

表达式EditorWorker
$("MQTT Client").item.json.publishedbooleanworker 不返回:读取结果为 undefined发布是否成功 —
$("MQTT Client").item.json.resultobject编辑器不提示= {…}—
$("MQTT Client").item.json.result.publishedboolean编辑器不提示= true—
$("MQTT Client").item.json.result.topicstring编辑器不提示= "plant/line-2/commands"—

— Editor: 编辑器会提示它,但 worker 并不返回:读取结果为 undefined。

— Worker: worker 会返回它,但编辑器不会提示:请手动输入。对象内部的键可能取决于你的数据或配置(例如每个标签一个键)。

错误

消息 端口 原因与处理
connection_id is required error 未选择 broker 凭据。
topic is required error 配置和输入 payload 中的主题均为空。
message is required error 消息为空。空字符串同样会失败:请至少发布一个字符。
Connection refused: Not authorized error broker 拒绝了凭据中的用户名或密码。

输入契约

connection_id string 必填
topic string 必填
message string 必填
retain boolean 可选
action string 可选

输出契约

published boolean 可选

发布是否成功

契约(JSON Schema)

与编排器声明完全一致的定义。All 是完整的 *.definition.js;可与上方各节对照,找出文档与 schema 之间的差异。发布前可用它校验节点。

{
  "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
    }
  }
}

执行

执行方式
async
有状态
否
凭据
在服务器端根据 connection_id 解析,密钥不会进入工作流。
能力
mqttiotmessaging
标签
iotmessaging

说明与限制

  • 使用客户端默认的 QoS 0 发布,未提供 QoS 选项。每次运行都会新建并关闭连接。
  • 输出契约将 published 声明在顶层,但 worker 将其放在 result 内返回。

已对照编排器代码核实 · 31e874b · 2026-11-26

安装智能体工具

为你的编程智能体接入 Sommatic AI 官方文档:技能包、文档 MCP 服务器与 Markdown 页面,无需再抓取网页。

仅安装技能包,不会自动更新;需要时重新运行即可。

npx skills add https://docs.sommatic.ai
智能体如何读取本文档