#Redis Pub/Sub using .Net and #Docker – The easy way

PNG-image-00D34A55B376-1.pngSo, you’ve heard about Redis and want to go kick the tires. How does one do Pub/Sub between two .Net clients with Redis in the middle?

The Redis/Docker Part:

  • Docker will host our Redis. Go to https://hub.docker.com , sign up and download the Docker software. Install it and login with your new Docker Hub account. It’ll take a few moments to start.
  • Open a Powershell and enter this Docker command to download and run Redis in docker on the standard port:

docker run -it -d -p 6379:6379    redis 

(Pro tip: Here’s how to enable local file usage for Redis to record data to: docker run -it -d -p 6379:6379 -v e:/docker/redis:/data redis  where e: is a drive you enabled access to via the Docker Dashboard )

  • Close Powershell, it’s not needed any more.

Redis is now running, to check it, open Docker Dashboard by right clicking the little whale icon in your system tray. Docker will give the Redis a funny random name:

2020-02-20 08_23_02-.png        2020-02-20 08_23_48-Container list.png

The Coding Part:

Open Visual Studio Twice and create 2 Console apps. One will publish and one will subscribe.

In each project, add the Nuget StackOverflow.Redis

In the publisher app, add this class and call it from Main():

using StackExchange.Redis;
using System;
using System.Threading.Tasks;
 
 
namespace RedisTest
{
    public class Publisher
    {
        private ConnectionMultiplexer redis;
        public Publisher()
        {
            redis = ConnectionMultiplexer.Connect("localhost");
        }
 
        public async Task PublisherMessages()
        {
            ISubscriber sub = redis.GetSubscriber();
            for (int i = 0; i < 1300; i++)
            {
                sub.Publish("message"Guid.NewGuid().ToString());
            }
        }
    }
}

In the subscriber app, add this class and call it from Main():

using StackExchange.Redis;
using System;
using System.Threading.Tasks;
 
 
namespace RedisTest
{
    public class Subscriber
    {
        private ConnectionMultiplexer redis;
        public Subscriber()
        {
            redis = ConnectionMultiplexer.Connect("localhost");
        }
 
        public async Task SubscriberMessages()
        {
            ISubscriber sub = redis.GetSubscriber();
            await sub.SubscribeAsync("message"async (channelvalue) =>
            {
                Console.WriteLine(value);
            });
        }
    }
}

Let’s run them!

Start up the Subscriber, then the Publisher. The publisher will pump the messages to Redis, Redis will them pump them to the subscriber. Even on a low powered system, you should be able to do 1500 messages a second. There are basically an unlimited number of subscribers and publishers.

Afterward:

Get a tool like Redily to allow you to view the data inside Redis.

 

 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s