/redis_messaging_library

Simple messaging (pub/sub) library using redis

Primary LanguageC#MIT LicenseMIT

A simple .NET standard library to make use of Redis pub/sub for messaging

Build PivotalServices.Redis.Messaging
Build Status NuGet

How to create a consumer/subscriber

    using PivotalServices.Redis.Messaging;
	
    public virtual void ConfigureServices(IServiceCollection services)
    {
        services.AddRedisMessagingConsumer();
    }
  • Next step is to subscribe to a channel to receive messages.
  • PivotalServices.Redis.Messaging.IConsumer implementation will be in the dependency container now.
  • Here, I would use the Configure method of startup.cs to demonstrate it (as below). But you can always inject PivotalServices.Redis.Messaging.IConsumer into any of the classes and perform the necessary operations there.
    using PivotalServices.Redis.Messaging;

    public void Configure(IApplicationBuilder app, IConsumer consumer) 
    {
        consumer.StartConsumption("myChannel", (message) =>
        {
            //Any action to be performed when a message is received
	    Console.Out.WriteLine($"Received Message, {message.Id}");
        });
    }
  • Similarly, to unsubscribe from a channel you can call StopConsumption as below, using IApplicationLifetime
    using PivotalServices.Redis.Messaging;

    public void Configure(IApplicationBuilder app, IApplicationLifetime lifetime, IConsumer consumer)
    {
        lifetime.ApplicationStopping.Register(() => consumer.StopConsumption("myChannel"));
    }

How to create a producer/publisher

    using PivotalServices.Redis.Messaging;
	
    public virtual void ConfigureServices(IServiceCollection services)
    {
        services.AddRedisMessagingProducer();
    }
  • Next step is to subscribe to a channel to receive messages.
  • PivotalServices.Redis.Messaging.IProducer implementation will be in the dependency container
  • I would use the Configure method of startup.cs to demonstrate it (as below - publish a message every 10 seconds). But you can always inject PivotalServices.Redis.Messaging.IProducer into any of the classes and publish messages there.
    using PivotalServices.Redis.Messaging;

    public void Configure(IApplicationBuilder app, IProducer producer)
    {
        while(true)
        {
            producer.Publish("myChannel", new Message(Guid.NewGuid().ToString(), DateTime.Now.ToString()));
            Thread.Sleep(10000);
        }
    }

Note: The channel name includes pattern matching, if it contains a *

Ongoing development packages in MyGet

Feed PivotalServices.Redis.Messaging
V3 MyGet

Issues

Contributions are welcome!