I am prototyping a redis client in golang using the redigo library to get notified of keyspace events. I modified the redis.conf to set the notify-keyspace-events to "KEA" to receive all events. But when I add/update/delete keys into the db using a cli, I don't see any events getting fired on the client.
Sample code that uses redigo to fire events:
type RedisClient struct {
mRedisServer string
mRedisConn redis.Conn
mWg sync.WaitGroup
}
func (rc *RedisClient) Run() {
conn, err := redis.Dial("tcp", ":6379")
if err != nil {
fmt.Println(err)
return
}
rc.mRedisConn = conn
fmt.Println(conn)
rc.mRedisConn.Do("CONFIG", "SET", "notify-keyspace-events", "KEA")
fmt.Println("Set the notify-keyspace-events to KEA")
defer rc.mRedisConn.Close()
rc.mWg.Add(2)
psc := redis.PubSubConn{Conn: rc.mRedisConn}
go func() {
defer rc.mWg.Done()
for {
switch msg := psc.Receive().(type) {
case redis.Message:
fmt.Printf("Message: %s %s\n", msg.Channel, msg.Data)
case redis.PMessage:
fmt.Printf("PMessage: %s %s %s\n", msg.Pattern, msg.Channel, msg.Data)
case redis.Subscription:
fmt.Printf("Subscription: %s %s %d\n", msg.Kind, msg.Channel, msg.Count)
if msg.Count == 0 {
return
}
case error:
fmt.Printf("error: %v\n", msg)
return
}
}
}()
go func() {
defer rc.mWg.Done()
psc.PSubscribe("\"__key*__:*\"")
select {}
}()
rc.mWg.Wait()
}
Does redigo support keyspace event notifications? Anything I maybe doing wrong here?
Remove the extra quotes in the subscribe pattern:
psc.PSubscribe("__key*__:*")
Also, you don't need the goroutines. It's simpler to write it as:
psc := redis.PubSubConn{Conn: rc.mRedisConn}
psc.PSubscribe("__key*__:*")
for {
switch msg := psc.Receive().(type) {
case redis.Message:
fmt.Printf("Message: %s %s\n", msg.Channel, msg.Data)
case redis.PMessage:
fmt.Printf("PMessage: %s %s %s\n", msg.Pattern, msg.Channel, msg.Data)
case redis.Subscription:
fmt.Printf("Subscription: %s %s %d\n", msg.Kind, msg.Channel, msg.Count)
if msg.Count == 0 {
return
}
case error:
fmt.Printf("error: %v\n", msg)
return
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With