下面显示的是英文原文。
Acknowledgement
An ack sends PUBACK at QoS 1 and PUBREC at QoS 2; QoS 0 has no acknowledgement.
A message settles when the handler returns, and the quality of service of the delivery decides what that sends.
At QoS 1 and 2 the server keeps a delivery until it is acknowledged, and the client finishes the QoS 2 handshake on its own once the server releases the message.
A QoS 0 delivery has no acknowledgement packet, so settling it answers AckError::Unsupported, which the runtime logs as a warning.
drop acknowledges the delivery too, because an acknowledgement is the only final answer MQTT has, and the server never sends the message again.
retry asks for a negative acknowledgement, which MQTT does not have, and retry_after is the outcome that tries again later.
The acknowledgement goes to the runner, which sends it; a delivery settled after the broker shut down answers AckError::Broker, carrying MqttError::NotConnected.
Example
A device agent applies the commands it knows and drops the rest; both answers settle the delivery.
- Macros
- Manual
/// The subscription reads at `QoS` 1, so returning from the handler acknowledges the delivery with
/// a PUBACK. A command the device does not know is dropped: on MQTT that acknowledges it as well,
/// and the server never sends it again.
#[subscriber(MqttTopic::new("devices/dev42/commands").qos(Qos::AtLeastOnce))]
async fn apply(command: &Command) -> HandlerOutcome {
match command.action.as_str() {
"reboot" | "reset" => HandlerOutcome::ack(),
_ => HandlerOutcome::drop(),
}
}
#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| b.include(apply))
}
/// The subscription reads at `QoS` 1, so returning from the handler acknowledges the delivery with
/// a PUBACK. A command the device does not know is dropped: on MQTT that acknowledges it as well,
/// and the server never sends it again.
struct Apply;
impl Handle<Command> for Apply {
fn handle(
&self,
command: &Command,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
ready(match command.action.as_str() {
"reboot" | "reset" => Ok(()),
_ => Err(HandlerOutcome::drop()),
})
}
}
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| {
let commands = MqttTopic::new("devices/dev42/commands").qos(Qos::AtLeastOnce);
b.include(
subscriber("devices/dev42/commands", Apply)
.map_source(|_| commands)
.build(),
)
})
}
QoS 0
The subscription reads telemetry at QoS 0. The handler settles each delivery as usual, and nothing is sent to the server.
- Macros
- Manual
/// Telemetry at `QoS` 0: the next reading replaces a lost one, so the subscription asks for no
/// acknowledgement. The outcome still settles the delivery in the service; the runtime has no
/// packet to send for it and logs that it is unsupported.
#[subscriber(MqttTopic::new("sensors/temp").qos(Qos::AtMostOnce))]
async fn store(reading: &Reading) -> HandlerOutcome {
if reading.celsius < -40 {
return HandlerOutcome::drop();
}
HandlerOutcome::ack()
}
#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| b.include(store))
}
/// Telemetry at `QoS` 0: the next reading replaces a lost one, so the subscription asks for no
/// acknowledgement. The outcome still settles the delivery in the service; the runtime has no
/// packet to send for it and logs that it is unsupported.
struct Store;
impl Handle<Reading> for Store {
fn handle(
&self,
reading: &Reading,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
ready(if reading.celsius < -40 {
Err(HandlerOutcome::drop())
} else {
Ok(())
})
}
}
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| {
let temp = MqttTopic::new("sensors/temp").qos(Qos::AtMostOnce);
b.include(
subscriber("sensors/temp", Store)
.map_source(|_| temp)
.build(),
)
})
}