use elbus::broker::{Broker, ServerConfig};
use elbus::client::AsyncClient;
use elbus::QoS;
use std::time::Duration;
use tokio::time::sleep;
const SLEEP_STEP: Duration = Duration::from_secs(1);
#[tokio::main]
async fn main() {
let mut broker = Broker::new();
broker.init_default_core_rpc().await.unwrap();
broker
.spawn_unix_server("/tmp/elbus.sock", ServerConfig::default())
.await
.unwrap();
let mut client1 = broker.register_client("worker.1").await.unwrap();
let mut client2 = broker.register_client("worker.2").await.unwrap();
let mut client3 = broker.register_client("worker.3").await.unwrap();
let rx = client2.take_event_channel().unwrap();
tokio::spawn(async move {
loop {
client1
.send("worker.2", "hello".as_bytes().into(), QoS::No)
.await
.unwrap();
sleep(SLEEP_STEP).await;
}
});
tokio::spawn(async move {
loop {
client3
.send_broadcast(
"worker.*",
"this is a broadcast message".as_bytes().into(),
QoS::No,
)
.await
.unwrap();
sleep(SLEEP_STEP).await;
}
});
while let Ok(frame) = rx.recv().await {
println!(
"{}: {}",
frame.sender(),
std::str::from_utf8(frame.payload()).unwrap()
);
}
}