Trait redis::PubSubCommands
source · [−]pub trait PubSubCommands: Sized {
fn subscribe<C, F, U>(&mut self, _: C, _: F) -> RedisResult<U>
where
F: FnMut(Msg) -> ControlFlow<U>,
C: ToRedisArgs;
fn psubscribe<P, F, U>(&mut self, _: P, _: F) -> RedisResult<U>
where
F: FnMut(Msg) -> ControlFlow<U>,
P: ToRedisArgs;
}
Expand description
The PubSub trait allows subscribing to one or more channels and receiving a callback whenever a message arrives.
Each method handles subscribing to the list of keys, waiting for messages, and unsubscribing from the same list of channels once a ControlFlow::Break is encountered.
Once (p)subscribe returns Ok(U), the connection is again safe to use for calling other methods.
Examples
use redis::{PubSubCommands, ControlFlow};
let client = redis::Client::open("redis://127.0.0.1/")?;
let mut con = client.get_connection()?;
let mut count = 0;
con.subscribe(&["foo"], |msg| {
// do something with message
assert_eq!(msg.get_channel(), Ok(String::from("foo")));
// increment messages seen counter
count += 1;
match count {
// stop after receiving 10 messages
10 => ControlFlow::Break(()),
_ => ControlFlow::Continue,
}
});
Required Methods
fn subscribe<C, F, U>(&mut self, _: C, _: F) -> RedisResult<U> where
F: FnMut(Msg) -> ControlFlow<U>,
C: ToRedisArgs,
fn subscribe<C, F, U>(&mut self, _: C, _: F) -> RedisResult<U> where
F: FnMut(Msg) -> ControlFlow<U>,
C: ToRedisArgs,
Subscribe to a list of channels using SUBSCRIBE and run the provided closure for each message received.
For every Msg
passed to the provided closure, either
ControlFlow::Break
or ControlFlow::Continue
must be returned. This
method will not return until ControlFlow::Break
is observed.
fn psubscribe<P, F, U>(&mut self, _: P, _: F) -> RedisResult<U> where
F: FnMut(Msg) -> ControlFlow<U>,
P: ToRedisArgs,
fn psubscribe<P, F, U>(&mut self, _: P, _: F) -> RedisResult<U> where
F: FnMut(Msg) -> ControlFlow<U>,
P: ToRedisArgs,
Subscribe to a list of channels using PSUBSCRIBE and run the provided closure for each message received.
For every Msg
passed to the provided closure, either
ControlFlow::Break
or ControlFlow::Continue
must be returned. This
method will not return until ControlFlow::Break
is observed.