Prash
Prash

Reputation: 11

How to access the keyspace notifications with ServiceStack.redis

I am trying to access the keyspace notifications in a .Net Application using ServiceStack.Redis. I am new to Redis.

I enabled event notifications on cache by command:

CONFIG SET notify-keyspace-events KEs

I am subscribing to the channel "key*:*" in .Net. The following is my code:

const string ChannelName = "__key*__:*";
    using (var redisConsumer = new RedisClient("localhost:6379"))
    using (var subscription = redisConsumer.CreateSubscription())
    {
        subscription.OnSubscribe = channel =>
        {
            Console.WriteLine(String.Format("Subscribed to '{0}'", channel));
        };
        subscription.OnUnSubscribe = channel =>
        {
            Console.WriteLine(String.Format("UnSubscribed from '{0}'", channel));
        };
        subscription.OnMessage = (channel, msg) =>
        {
            Console.WriteLine(String.Format("Received '{0}' from channel '{1}'", 
                msg, channel));
        };             

        Console.WriteLine(String.Format("Started Listening On '{0}'", ChannelName));
        subscription.SubscribeToChannels(ChannelName); //blocking
   }

From another .Net application, I am adding new data to the cache. I am expecting to receive an event (in OnMessage). The application is not capturing any event, on adding a new item in cache.

But, when I run the command "psubscribe 'key*:*'" on redis-cli.exe, it captures the events properly. ( when I add a new item to cache, it displays the event details in a console window.)

I am unable to capture the same in my application. Am I missing anything here?

Upvotes: 1

Views: 678

Answers (1)

Binz
Binz

Reputation: 36

use subscription.SubscribeToChannelsMatching(ChannelName);

Upvotes: 1

Related Questions