Skip to content

Commit 3fbd9dd

Browse files
authored
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
1 parent f187085 commit 3fbd9dd

File tree

7 files changed

+362
-21
lines changed

7 files changed

+362
-21
lines changed

Cargo.lock

Lines changed: 22 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ tokio = { git = "https://github.com/tokio-rs/tokio", features = ["full"] }
1212
tracing = "0.1.13"
1313
tracing-futures = { version = "0.2.3", features = ["tokio"] }
1414
tracing-subscriber = "0.2.2"
15+
async-stream = "0.2.1"
1516

1617
[dev-dependencies]
1718
# Enable test-utilities in dev mode only. This is mostly for tests.

examples/pub.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,31 @@
1+
//! Publish to a redis channel example.
2+
//!
3+
//! A simple client that connects to a mini-redis server, and
4+
//! publishes a message on `foo` channel
5+
//!
6+
//! You can test this out by running:
7+
//!
8+
//! cargo run --bin server
9+
//!
10+
//! Then in another terminal run:
11+
//!
12+
//! cargo run --example sub
13+
//!
14+
//! And then in another terminal run:
15+
//!
16+
//! cargo run --example pub
17+
18+
#![warn(rust_2018_idioms)]
19+
20+
use mini_redis::{client, Result};
21+
122
#[tokio::main]
2-
async fn main() {
3-
unimplemented!();
23+
async fn main() -> Result<()> {
24+
// Open a connection to the mini-redis address.
25+
let mut client = client::connect("127.0.0.1:6379").await?;
26+
27+
// publish message `bar` on channel foo
28+
client.publish("foo", "bar".into()).await?;
29+
30+
Ok(())
431
}

examples/sub.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,37 @@
1-
/// Subscribe to a redis channel
1+
//! Subscribe to a redis channel example.
2+
//!
3+
//! A simple client that connects to a mini-redis server, subscribes to "foo" and "bar" channels
4+
//! and awaits messages published on those channels
5+
//!
6+
//! You can test this out by running:
7+
//!
8+
//! cargo run --bin server
9+
//!
10+
//! Then in another terminal run:
11+
//!
12+
//! cargo run --example sub
13+
//!
14+
//! And then in another terminal run:
15+
//!
16+
//! cargo run --example pub
17+
18+
#![warn(rust_2018_idioms)]
19+
20+
use mini_redis::{client, Result};
221

322
#[tokio::main]
4-
async fn main() {
5-
unimplemented!();
23+
pub async fn main() -> Result<()> {
24+
// Open a connection to the mini-redis address.
25+
let client = client::connect("127.0.0.1:6379").await?;
26+
27+
28+
// subscribe to channel foo
29+
let mut subscriber = client.subscribe(vec!["foo".into()]).await?;
30+
31+
// await messages on channel foo
32+
let msg = subscriber.next_message().await? ;
33+
println!("got message from the channel: {}; message = {:?}", msg.channel, msg.content);
34+
35+
36+
Ok(())
637
}

0 commit comments

Comments
 (0)