Request/reply for MQTT 3.1.1 via explicit reply topic.
The caller must supply cmd.reply_to. FastStream subscribes to that topic, publishes the raw request payload, then waits for the first message on the reply topic. The handler side must publish its response to the same topic (e.g. via @broker.publisher).
Source code in faststream/mqtt/publisher/producer.py
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147 | @override
async def request(self, cmd: "MQTTPublishCommand") -> "zmqtt.Message":
"""Request/reply for MQTT 3.1.1 via explicit reply topic.
The caller must supply ``cmd.reply_to``. FastStream subscribes to
that topic, publishes the raw request payload, then waits for the
first message on the reply topic. The handler side must publish
its response to the same topic (e.g. via ``@broker.publisher``).
"""
if not cmd.reply_to:
msg = "MQTT 3.1.1 request() requires an explicit reply_to topic."
raise FeatureNotSupportedException(msg)
sub = self._connected_client.subscribe(cmd.reply_to)
await sub.start()
try:
payload, _ = await self.codec.encode(cmd.body, self.serializer)
await self._connected_client.publish(
cmd.destination,
payload,
qos=cmd.qos,
retain=cmd.retain,
)
return await asyncio.wait_for(
sub.get_message(),
timeout=cmd.timeout or 30.0,
)
finally:
await sub.stop()
|