use darkredis::ConnectionPool;
use futures::StreamExt;
use std::time::Duration;
#[cfg(feature = "runtime_async_std")]
fn main() {
println!("This example is only compatible with Tokio.")
}
#[cfg(feature = "runtime_tokio")]
#[tokio::main]
async fn main() {
let pool = ConnectionPool::create("127.0.0.1:6379".into(), None, 1)
.await
.unwrap();
let channels = vec!["some-channel", "some-other-channel"];
let listener = pool.spawn("mylistener").await.unwrap();
let messagestream = listener.subscribe(&channels).await.unwrap();
tokio::spawn(async move {
let mut publisher = pool.get().await;
let mut interval = tokio::time::interval(Duration::from_secs(1));
loop {
publisher.publish("some-channel", "hello!").await.unwrap();
publisher
.publish("some-other-channel", "hello again!")
.await
.unwrap();
interval.tick().await; }
});
messagestream
.for_each(|e| async move {
println!(
"Received a message on channel '{}': {}",
String::from_utf8_lossy(&e.channel),
String::from_utf8_lossy(&e.message)
);
})
.await;
}