简体   繁体   中英

StackExchange.Redis not retrieving the matched pattern when I receive the delegate

I'm implementing signalR that is feed by the Pub/Sub of Redis. To interact with Redis I've used StackExchange.Redis-1.2.6.

The issue here is that when I subscribe a pattern on the signalR hub I create a group with ConnectionId and topic that I'm interested and do the same on Redis Pub/Sub.

When I receive the message I need to trace back and notify all the interested subscribers, but the problem is that Redis is not giving me the matched pattern but the published topic instead.

Here is the code sample:

        ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
        ISubscriber sub = redis.GetSubscriber();

        RedisChannel channelWithLiteral = new RedisChannel("messages", RedisChannel.PatternMode.Literal);
        sub.Subscribe(channelWithLiteral, (channel, message) => 
        {
            Console.WriteLine($"Literal -> channel: '{channel}' message: '{message}'");
        });

        RedisChannel channelWithPattern = new RedisChannel("mess*", RedisChannel.PatternMode.Pattern);
        sub.Subscribe(channelWithPattern, (channel, message) => 
        {
            Console.WriteLine($"Pattern -> channel: '{channel}' message: '{message}'");
        });

        sub.Publish("messages", "hello");
        Console.ReadLine();

The output is:

Literal -> channel: 'messages' message: 'hello'

Pattern -> channel: 'messages' message: 'hello'

What I was expecting/needed:

Literal -> channel: 'messages' message: 'hello'

Pattern -> channel: 'mess*' message: 'hello'

On https://redis.io/topics/pubsub it says when using PSUBSCRIBE we are notified of both: original topic, and matched pattern.

Here is the example:

在此处输入图片说明

Is there any way on StackExchange.Redis to receive the matched pattern?

I've opened an issue at GitHub: RedisChannel is not retrieving the matched pattern when I receive the delegate :

You can see there a workaround solution for the time being.

" Right now, the only way to do this would be to "capture" the subscription channel when subscribing. "

Here is the workaround approach:

  1. Before creating the Action delegate, declare a temporary variable to store the pattern On the delegate Action reference that temporary variable

  2. Each time someone tries to subscribe will create a new instance of that variable and deliver that info together with Redis channel

Here is the code example:

public class MyTest
    {
        private readonly ISubscriber subscriber; 

        public MyTest()
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("localhost");
            this.subscriber = redis.GetSubscriber();
        }

        public void SubscribeWithPattern(string pattern)
        {
            RedisChannel channelWithPattern = new RedisChannel(pattern, RedisChannel.PatternMode.Pattern);
            string originalChannelSubcription = pattern;
            this.subscriber.Subscribe(channelWithPattern, (channel, message) =>
            {
                Console.WriteLine($"OriginalChannelSubcription '{originalChannelSubcription}");
                Console.WriteLine($"Channel: '{channel}");
                Console.WriteLine($"Message: '{message}'");
            });
        }

        public void Publish(string channel, string message)
        {
            this.subscriber.Publish(channel, message);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM