Skip to content

MQTT

MQTT binding。輸出的成員是 mqtt,每個物件都帶 bindingVersion: 0.2.0

@mqttServer

typespec
extern dec mqttServer(target: Namespace, config: valueof AsyncAPIMqttServerBinding);
欄位型別必填
clientIdstring
cleanSessionboolean
lastWillAsyncAPIMqttLastWill
keepAliveint32
sessionExpiryIntervalunknown
maximumPacketSizeunknown

套用在service namespace 上。該 namespace 宣告的每一個 server 各拿到一份。

lastWill 是用戶端沒送 DISCONNECT 就斷線時,broker 代發的 message。它的 qos012。其他值會回報 invalid-binding-field,丟棄該欄位,其餘欄位保留。

sessionExpiryIntervalmaximumPacketSize 是 MQTT 5 欄位。寫成數字,或寫成描述該數字的 Schema Object。

typespec
@service(#{ title: "Sensors" })
@server("prod", #{ host: "mqtt.example.com:8883", protocol: "mqtt" })
@mqttServer(#{
  clientId: "sensor-gateway",
  cleanSession: true,
  keepAlive: 60,
  lastWill: #{ topic: "sensors/status", qos: 1, message: "offline", retain: true }
})
namespace Sensors;
yaml
servers:
  prod:
    host: mqtt.example.com:8883
    protocol: mqtt
    bindings:
      mqtt:
        clientId: sensor-gateway
        cleanSession: true
        lastWill:
          topic: sensors/status
          qos: 1
          message: offline
          retain: true
        keepAlive: 60
        bindingVersion: 0.2.0

@mqttOperation

typespec
extern dec mqttOperation(target: Operation, config: valueof AsyncAPIMqttOperationBinding);
欄位型別必填
qosint32
retainboolean
messageExpiryIntervalunknown

套用在帶有 @send@receive 的 operation 上。

qos012。其他值會被回報並丟棄。

typespec
@send
@mqttOperation(#{ qos: 1, retain: false, messageExpiryInterval: 3600 })
op publish(event: Reading): void;
yaml
operations:
  publish:
    action: send
    channel:
      $ref: "#/channels/sensors~1reading"
    bindings:
      mqtt:
        qos: 1
        retain: false
        messageExpiryInterval: 3600
        bindingVersion: 0.2.0

@mqttMessage

typespec
extern dec mqttMessage(target: Model, config: valueof AsyncAPIMqttMessageBinding);
欄位型別必填
payloadFormatIndicatorint32
correlationDataunknown
contentTypestring
responseTopicunknown

套用在同時帶有 @message 的 model 上。四個欄位都是 MQTT 5 欄位。

payloadFormatIndicator0 表示未指定的位元組,1 表示 UTF-8。correlationData 是 Schema Object。responseTopic 是 topic 名稱,或描述該名稱的 Schema Object。

typespec
@message
@mqttMessage(#{
  payloadFormatIndicator: 1,
  contentType: "application/json",
  responseTopic: "sensors/reply"
})
model Reading {
  value: float64;
}
yaml
components:
  messages:
    Reading:
      name: Reading
      payload:
        $ref: "#/components/schemas/Reading"
      bindings:
        mqtt:
          payloadFormatIndicator: 1
          contentType: application/json
          responseTopic: sensors/reply
          bindingVersion: 0.2.0