Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add pub sub client implementation with examples #22

Merged
merged 6 commits into from
Apr 5, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ tokio = { git = "https://github.com/tokio-rs/tokio", features = ["full"] }
tracing = "0.1.13"
tracing-futures = { version = "0.2.3", features = ["tokio"] }
tracing-subscriber = "0.2.2"
async-stream = "0.2.1"

[dev-dependencies]
# Enable test-utilities in dev mode only. This is mostly for tests.
Expand Down
31 changes: 29 additions & 2 deletions examples/pub.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
37 changes: 34 additions & 3 deletions examples/sub.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
Loading