MQTT Client
向 MQTT Broker 发布消息。
连接 MQTT broker,向一个主题发布一条消息后断开连接。
工作原理
- 解析配置。
connection_id、topic、message和retain(默认 false)缺失时回退到输入 payload 中的同名键;前三项为必填。 - 解析凭据:broker URL 为
config.url,或mqtt://<config.host>:<config.port>(端口默认 1883);密钥中存在username和password时使用它们。 - 建立连接,带
retain标志向topic发布message,然后关闭连接。 - 从
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
}
}{
"ok": true,
"data": {
"result": {
"published": true,
"topic": "plant/line-2/commands"
}
},
"port": "out"
}配置
-
connection_idstring 必填 -
MQTT Broker 的凭据。接受 `basic-auth`(用户名 + 密码 + 主机 + 端口)、`connection-string`(mqtt:// / mqtts:// URI)或 `object`(高级 MQTT 配置)类型的凭据。
-
topicstring 必填 -
messagestring 必填 -
消息载荷
-
retainboolean 可选 -
actionstring 可选
代码中的行为
除 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 返回的路径,依据“端口”一节所示的输出。
| 表达式 | Editor | Worker |
|---|---|---|
$("MQTT Client").item.json.published发布是否成功 | — | |
$("MQTT Client").item.json.result | — | |
$("MQTT Client").item.json.result.published | — | |
$("MQTT Client").item.json.result.topic | — |
— 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_idstring 必填 -
topicstring 必填 -
messagestring 必填 -
retainboolean 可选 -
actionstring 可选
输出契约
-
publishedboolean 可选 -
发布是否成功
契约(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
}
}
}{
"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"
]
}{
"type": "object",
"properties": {
"connection_id": {
"type": "string"
},
"topic": {
"type": "string"
},
"message": {
"type": "string"
},
"retain": {
"type": "boolean"
},
"action": {
"type": "string"
}
},
"required": [
"connection_id",
"topic",
"message"
]
}{
"type": "object",
"properties": {
"published": {
"type": "boolean",
"description": "Publish success"
}
}
}[
{
"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
}
]执行
- 执行方式
async- 有状态
- 否
- 凭据
- 在服务器端根据
connection_id解析,密钥不会进入工作流。 - 能力
mqttiotmessaging- 标签
iotmessaging
说明与限制
- 使用客户端默认的 QoS 0 发布,未提供 QoS 选项。每次运行都会新建并关闭连接。
- 输出契约将
published声明在顶层,但 worker 将其放在result内返回。
已对照编排器代码核实 · 31e874b · 2026-11-26