add pub sub client implementation with examples (#22)

* add pub sub client implementation with examples

* replace subscribed_channels list Vec with HashSet to avoid duplicates

* update Subscriber to use async-stream instead of manual Stream impl

* revert update to error handling server.rs, as #21 handles it

* remove uneeded recursion limit extension
This commit is contained in:
João Oliveira
2020-04-05 18:33:21 +01:00
committed by GitHub
parent f187085156
commit 3fbd9ddc42
7 changed files with 363 additions and 22 deletions

View File

@@ -1,4 +1,31 @@
//! Publish to a redis channel example.
//!
//! A simple client that connects to a mini-redis server, and
//! publishes a message on `foo` channel
//!
//! You can test this out by running:
//!
//! cargo run --bin server
//!
//! Then in another terminal run:
//!
//! cargo run --example sub
//!
//! And then in another terminal run:
//!
//! cargo run --example pub
#![warn(rust_2018_idioms)]
use mini_redis::{client, Result};
#[tokio::main]
async fn main() {
unimplemented!();
async fn main() -> Result<()> {
// Open a connection to the mini-redis address.
let mut client = client::connect("127.0.0.1:6379").await?;
// publish message `bar` on channel foo
client.publish("foo", "bar".into()).await?;
Ok(())
}

View File

@@ -1,6 +1,37 @@
/// Subscribe to a redis channel
//! Subscribe to a redis channel example.
//!
//! A simple client that connects to a mini-redis server, subscribes to "foo" and "bar" channels
//! and awaits messages published on those channels
//!
//! You can test this out by running:
//!
//! cargo run --bin server
//!
//! Then in another terminal run:
//!
//! cargo run --example sub
//!
//! And then in another terminal run:
//!
//! cargo run --example pub
#![warn(rust_2018_idioms)]
use mini_redis::{client, Result};
#[tokio::main]
async fn main() {
unimplemented!();
pub async fn main() -> Result<()> {
// Open a connection to the mini-redis address.
let client = client::connect("127.0.0.1:6379").await?;
// subscribe to channel foo
let mut subscriber = client.subscribe(vec!["foo".into()]).await?;
// await messages on channel foo
let msg = subscriber.next_message().await? ;
println!("got message from the channel: {}; message = {:?}", msg.channel, msg.content);
Ok(())
}