Shared subscription
A shared subscription hands each message to one member of a group, so boards that run the same service share the work.
shared(group) on MqttTopic or MqttFilter subscribes through $share/<group>/<filter>, an MQTT 5 feature.
The server gives each matching message to one subscription of the group instead of to every one.
The group name must not be empty and must not contain /, + or #; the descriptor refuses it when the app starts.
The server sends no retained message to a shared subscription when it joins.
A retry copy of a delivery goes to the plain topic: the $share prefix is a name for subscribing, and a publish to it would reach a topic of that literal spelling.
Example
Every board runs this service, and the server gives each job to one of them.
- Macros
- Manual
/// Every board runs this service; the server gives each job to one of them.
#[subscriber(MqttTopic::new("jobs/render").shared("renderers"))]
async fn render(job: &Job) -> HandlerOutcome {
let _ = job.id;
HandlerOutcome::ack()
}
#[ruststream::app(runtime = embassy)]
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| b.include(render))
}
/// Every board runs this service; the server gives each job to one of them.
struct Render;
impl Handle<Job> for Render {
fn handle(
&self,
_job: &Job,
_outs: &(),
_ctx: &mut Context<'_>,
) -> impl Future<Output = Result<(), HandlerOutcome>> {
ready(Ok(()))
}
}
fn app(broker: MqttBroker) -> EmbeddedApp<impl Mounts> {
EmbeddedApp::new().with_broker(broker, |b| {
b.include(
subscriber("jobs/render", Render)
.map_source(|_| MqttTopic::new("jobs/render").shared("renderers"))
.build(),
)
})
}