By default, keyspace events notifications are disabled. We can enable them in redis.conf or redis-cli as below:
$ redis-cli config set notify-keyspace-events KEA
$ OK
extern crate redis; | |
fn listen_redis_notifications() -> redis::RedisResult<()> { | |
let client = try!(redis::Client::open("redis://127.0.0.1/0")); | |
let mut pubsub = try!(client.get_pubsub()); | |
try!(pubsub.psubscribe("__keyspace@0__:*")); | |
try!(pubsub.psubscribe("__keyevent@0__:*")); | |
loop { | |
let msg = try!(pubsub.get_message()); | |
let payload : String = try!(msg.get_payload()); | |
println!("Channel '{}': {}", msg.get_channel_name(), payload); | |
} | |
} | |
fn main() { | |
match listen_redis_notifications() { | |
Err(why) => panic!("{:?}", why), | |
Ok(t) => println!("ok: {:?}", t), | |
} | |
} |