Skip to content

ZmqttProducerV5

faststream.mqtt.publisher.producer.ZmqttProducerV5 #

ZmqttProducerV5(
    parser: Optional[CustomCallable],
    decoder: Optional[CustomCallable],
    id_generator: IdGenerator = gen_cor_id,
)

Bases: ZmqttBaseProducer

Producer for MQTT 5.0 — publishes with PublishProperties.

Source code in faststream/mqtt/publisher/producer.py
155
156
157
158
159
160
161
def __init__(
    self,
    parser: Optional["CustomCallable"],
    decoder: Optional["CustomCallable"],
    id_generator: IdGenerator = gen_cor_id,
) -> None:
    super().__init__(MQTTParserV5(), parser, decoder, id_generator)

codec instance-attribute #

codec: CodecProto = DefaultCodec()

serializer instance-attribute #

serializer: SerializerProto | None = None

id_generator instance-attribute #

id_generator = id_generator

publish async #

publish(cmd: MQTTPublishCommand) -> None
Source code in faststream/mqtt/publisher/producer.py
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
@override
async def publish(self, cmd: "MQTTPublishCommand") -> None:
    payload, content_type = await self.codec.encode(cmd.body, self.serializer)

    user_props: list[tuple[str, str]] = [
        (k, str(v)) for k, v in (cmd.headers or {}).items()
    ]

    properties = PublishProperties(
        content_type=content_type or None,
        response_topic=cmd.reply_to or None,
        correlation_data=cmd.correlation_id.encode() if cmd.correlation_id else None,
        user_properties=tuple(user_props),
        message_expiry_interval=cmd.message_expiry_interval,
    )

    await self._connected_client.publish(
        cmd.destination,
        payload,
        qos=cmd.qos,
        retain=cmd.retain,
        properties=properties,
    )

request async #

request(cmd: MQTTPublishCommand) -> Message

Request/reply for MQTT 5.0 via zmqtt's native client.request().

zmqtt auto-generates a unique reply topic. We pass our correlation ID explicitly so the responder echoes it back and the caller can verify it on the response StreamMessage.

Source code in faststream/mqtt/publisher/producer.py
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
@override
async def request(self, cmd: "MQTTPublishCommand") -> "zmqtt.Message":
    """Request/reply for MQTT 5.0 via zmqtt's native client.request().

    zmqtt auto-generates a unique reply topic.  We pass our correlation
    ID explicitly so the responder echoes it back and the caller can
    verify it on the response StreamMessage.
    """
    payload, content_type = await self.codec.encode(cmd.body, self.serializer)
    correlation_id = cmd.correlation_id or self.id_generator()

    user_props: list[tuple[str, str]] = [
        (k, str(v)) for k, v in (cmd.headers or {}).items()
    ]

    # Pass correlation_data explicitly so the responder echoes it back.
    # Do NOT set response_topic — let zmqtt generate it.
    properties = PublishProperties(
        content_type=content_type or None,
        correlation_data=correlation_id.encode(),
        user_properties=tuple(user_props),
        message_expiry_interval=cmd.message_expiry_interval,
    )

    return await self._connected_client.request(
        cmd.destination,
        payload,
        qos=cmd.qos,
        timeout=cmd.timeout or 30.0,
        properties=properties,
    )

publish_batch async #

publish_batch(cmd: MQTTPublishCommand) -> None
Source code in faststream/mqtt/publisher/producer.py
81
82
83
84
@override
async def publish_batch(self, cmd: "MQTTPublishCommand") -> None:
    msg = "MQTT does not support batch publishing."
    raise FeatureNotSupportedException(msg)

connect #

connect(
    client: MQTTClient,
    serializer: Optional[SerializerProto],
    codec: Optional[CodecProto] = None,
) -> None
Source code in faststream/mqtt/publisher/producer.py
52
53
54
55
56
57
58
59
60
def connect(
    self,
    client: "zmqtt.MQTTClient",
    serializer: Optional["SerializerProto"],
    codec: Optional["CodecProto"] = None,
) -> None:
    self._client = client
    self.serializer = serializer
    self.codec = codec or DefaultCodec()

disconnect #

disconnect() -> None
Source code in faststream/mqtt/publisher/producer.py
62
63
64
def disconnect(self) -> None:
    self._client = None
    self.serializer = None