Wildcard Subscription
Subscribe to multiple channels using wildcard patterns
Overview
A wildcard subscription lets one subscriber match a whole family of channels with a single call, instead of wiring up a separate subscribe_to_events for every sub-channel and touching code each time a new one appears. It's the natural fit for monitoring, logging, or fan-in aggregation across a channel hierarchy — for example, watching every regional order channel from one place.
KubeMQ matches wildcard tokens against the channel hierarchy server-side at delivery time. * matches exactly one dot-separated segment, and > matches one or more trailing segments, so client.subscribe_to_events("rust-events.wildcard.*", ...) catches any single-segment suffix. Every delivered event still carries its exact channel, so the callback can tell which concrete sub-channel it came from even though the subscription itself only named a pattern.
Gotchas: * matches exactly one segment — it won't reach two levels deep, so orders.* misses orders.us.east; use > for that. Wildcards are only valid on Events subscriptions, not on send_event/publishes or on events-store, queues, or commands/queries. And an overly broad pattern like > at the root will quietly pull in every channel under that prefix, including ones you didn't intend to monitor.
Prerequisites
- KubeMQ server running on
localhost:50000 - Rust SDK installed (
cargo add kubemq)
Code
use kubemq::prelude::*;
use kubemq::EventBuilder;
use std::time::Duration;
#[tokio::main]
async fn main() -> kubemq::Result<()> {
let client = KubemqClient::builder()
.host("localhost")
.port(50000)
.build()
.await?;
let sub = client
.subscribe_to_events(
"rust-events.wildcard.*",
"",
|event| {
Box::pin(async move {
println!(
"Wildcard received: channel={}, body={}",
event.channel,
String::from_utf8_lossy(&event.body)
);
})
},
None,
)
.await?;
tokio::time::sleep(Duration::from_millis(500)).await;
for suffix in &["orders", "users", "logs"] {
let channel = format!("rust-events.wildcard.{}", suffix);
let event = EventBuilder::new()
.channel(&channel)
.body(format!("message for {}", suffix).into_bytes())
.build();
client.send_event(event).await?;
println!("Sent event to {}", channel);
}
tokio::time::sleep(Duration::from_secs(2)).await;
sub.unsubscribe().await;
client.close().await?;
Ok(())
}How It Works
- Wildcard patterns (
*,>) are supported on subscriptions but not on sends. *matches a single segment;>matches one or more segments.- Review timeouts, channel names, and client IDs before running against shared environments.
- Run the program while the server from the prerequisites is available.
Related
Was this page helpful?