-
Notifications
You must be signed in to change notification settings - Fork 486
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add pub sub client implementation with examples
- Loading branch information
Showing
6 changed files
with
316 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,38 @@ | ||
/// 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}; | ||
use tokio::stream::StreamExt; | ||
|
||
#[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 result = client.subscribe(vec!["foo".into()]).await?; | ||
|
||
// await messages on channel foo | ||
while let Some(Ok(msg)) = result.next().await { | ||
println!("got message from the channel: {}; message = {:?}", msg.channel, msg.content); | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.