Publishing over MQTT#
FastStream uses the same publishing patterns as in the getting started guide: await broker.publish(...), @broker.publisher(...), and publisher objects.
MQTTBroker.publish#
Key arguments:
| Argument | Description |
|---|---|
message | Body (SendableMessage): primitives, models, bytes, etc. |
topic | Target topic (must not contain + or #). |
qos | QoS.AT_MOST_ONCE (0), AT_LEAST_ONCE (1), or EXACTLY_ONCE (2). |
retain | If true, the broker stores the last message for new subscribers. |
headers | MQTT 5.0 only — maps to User Properties. |
correlation_id | MQTT 5.0 only — Correlation Data (for tracing or paired replies). |
reply_to | MQTT 5.0 only — Response Topic for request/reply style flows. |
MQTT 3.1.1 rejects headers, correlation_id, and reply_to; use MQTT 5.0 for metadata on the wire.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | |
Publisher objects#
broker.publisher("topic", qos=..., retain=..., headers=...) returns an MQTTPublisher with the same semantics. Per-call publish() can override qos / retain / headers where applicable.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Publisher decorator#
Decorated subscriber return values are published to the configured topic, the same as other FastStream brokers.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
Batching#
MQTT has no batch publish in FastStream — calling batch APIs raises FeatureNotSupportedException.
Publish failures#
FastStream propagates zmqtt's publish exceptions to callers of broker.publish() and publisher objects:
zmqtt.MQTTPublishErrorreports a rejected QoS 1/2 PUBACK or PUBREC. Itsreason_code,reason_name, and optionalreason_stringdescribe the rejection.zmqtt.MQTTQoSExceededErrormeans the requested publish QoS exceeds the server's MQTT 5.0 Maximum QoS. The driver rejects the publish before sending it; it does not silently lower QoS. The exception exposesrequested_qosandmaximum_qos.
The raw server limit is available as broker.connection_info.properties.maximum_qos when CONNACK properties are present. An omitted Maximum QoS means QoS 2 is allowed.