Skip to content

Instantly share code, notes, and snippets.

@tyler46
Last active April 21, 2022 07:50
Show Gist options
  • Save tyler46/128f9dece7a23bb964d485a1d8408fe9 to your computer and use it in GitHub Desktop.
Save tyler46/128f9dece7a23bb964d485a1d8408fe9 to your computer and use it in GitHub Desktop.
Redis keyspace notification in Rust

Enable keyspace notifications

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),
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment