use anyhow::Context as _;
use tokio::sync::mpsc;
pub const DEFAULT_URL: &str = "nats://127.0.0.1:4222";
pub async fn connect(addrs: impl async_nats::ToServerAddrs) -> anyhow::Result<async_nats::Client> {
let (conn_tx, mut conn_rx) = mpsc::channel(1);
let client = async_nats::connect_with_options(
addrs,
async_nats::ConnectOptions::new()
.retry_on_initial_connect()
.event_callback(move |event| {
let conn_tx = conn_tx.clone();
async move {
if let async_nats::Event::Connected = event {
conn_tx
.send(())
.await
.expect("failed to send NATS.io server connection notification");
}
}
}),
)
.await
.context("failed to connect to NATS.io server")?;
conn_rx
.recv()
.await
.context("failed to await NATS.io server connection to be established")?;
Ok(client)
}