Publish and Subscribe messages with Redis – C# .NET Core

Publish and Subscribe messages with Redis

Today in this article, we shall see how to Publish and Subscribe messages with Redis using C# .NET Core.

Redis is widely regarded as the most efficient cache tool for stateless back-end app servers.

Many people are however unaware that apart from an excellent cache server, it also provides built-in Publishing / Subscribe / Messaging functionality helping developers create event-driven based applications, microservices, etc.

Today in this article, we will cover below aspects,

Today in this article, we shall try to cover the basics pub-sub model and how to get started.

Getting started

Let’s start creating .NET Core console application as below,

Redis subscriber example C

Please install the Redis NuGet package Microsoft.Extensions.Caching.StackExchangeRedis

as below,

> Install-Package Microsoft.Extensions.Caching.StackExchangeRedis -Version <version>

Publish/Subscribe messages with Redis – subscriber example C#

Below shows a simple example for Redis subscribers.

            using ConnectionMultiplexer redis = 
            ConnectionMultiplexer.Connect(redisConnectionString);
            ISubscriber subScriber = redis.GetSubscriber();
            //Subscribe to the channel "thecode-buzz-channel"
            subScriber.Subscribe(publishChannel, (channel, message) =>
            {
                //Output received message
                Console.WriteLine($"[{DateTime.Now:HH:mm:ss}]: {$"Message {message} received successfully"}");
            });

Above we are using the ConnectionMultiplexer interface to create subscriber objects to subscribe to the name channel.

Publish/Subscribe messages with Redis – Publisher example c#

Below shows a simple example for Redis Publisher,

            using ConnectionMultiplexer redis = 
            ConnectionMultiplexer.Connect(redisConnectionString);
            ISubscriber sub = redis.GetSubscriber();
            for (int i = 0; i <= 10; i++)
            {
                sub.Publish(publishChannel, $"Message {i}");
                Console.WriteLine($"Message {i} published successfully");
                Thread.Sleep(2000);
            }

ConnectionMultiplexer lets you create publisher objects to create a publisher which can publish the business events to the registered named channel.

For example, we are simply publishing 10 messages to the Redis channel.

Redis Publisher example c

All the clients which are subscribed for the updates will receive updates when the message is published to the channel successfully.

ConnectionMultiplexer interface can be used via dependency injection providing better control on lifetime management of the instance. Please visit this article for more details.

References:

That’s all! Happy coding!

Does this help you fix your issue?

Do you have any better solutions or suggestions? Please sound off your comments below.



Please bookmark this page and share it with your friends. Please Subscribe to the blog to receive notifications on freshly published(2024) best practices and guidelines for software design and development.



Leave a Reply

Your email address will not be published. Required fields are marked *