#![allow(clippy::print_stdout, clippy::print_stderr)]
use std::time::Duration;
use cent::{
BatchOptions, BroadcastRequest, BroadcastRetryOptions, Client, Config, Error, PublishRequest,
};
use serde_json::json;
#[tokio::main]
async fn main() -> Result<(), Error> {
let endpoint =
std::env::var("CENTRIFUGO_API").unwrap_or_else(|_| "http://localhost:8000/api".into());
let key = std::env::var("CENTRIFUGO_API_KEY").unwrap_or_default();
let client = Client::new(Config::new(endpoint).api_key(key))?;
let data = cent::json(&json!({"text": "hello"}))?;
let res = client
.publish(PublishRequest::new("news").data(data.clone()))
.await?;
println!("published at offset {} in epoch {}", res.offset, res.epoch);
let request = BroadcastRequest::new(["user:1", "user:2", "unknown:3"]).data(data.clone());
match client
.broadcast_with_retry(request, BroadcastRetryOptions::default())
.await
{
Ok(res) => println!("broadcast into {} channels", res.channels.len()),
Err(Error::BroadcastFailedChannels(e)) => {
for ch in e.failed() {
if let Err(err) = &ch.result {
eprintln!("not published into {}: {err}", ch.channel);
}
}
}
Err(e) => return Err(e), }
let mut batch = client.new_batch(BatchOptions::default());
let publication = batch.publish(PublishRequest::new("news").data(data));
let stats = batch.presence_stats(cent::PresenceStatsRequest::new("news"));
if let Err(e) = batch.send().await {
eprintln!("batch: {e}");
}
println!(
"offset {:?}, stats {:?}",
publication.result().map(|r| r.offset),
stats.result().map(|s| s.num_clients)
);
client.close(Duration::from_secs(10)).await
}