How to show message after REDIS subscrive to a Channel or from a Queue?

0
1

Hi,

I’m trying to create a background service capable of subscribing a message listening to a REDIS channel (would be the same for a classic RABBIT-type message queue).
Steps:

  1. I created an object and a connection to REDIS in Startup.cs
    Application.Services.AddService<IConnectionMultiplexer>( x => ConnectionMultiplexer.Connect("192.168.1.178:6379"));

    this allows me to inject the object between my instances.

  2. I created a class derived from BackgroundService and in the ExecuteAsync event I subscribe to the REDIS “messages” channel :
    // Injected
    connectionMultiplexer = Application.Services.GetService<IConnectionMultiplexer>();
    
    // Event from REDIS 
    protected override Task ExecuteAsync(CancellationToken stoppingToken) {
       var subscriver = connectionMultiplexer.GetSubscriber();
    
       return subscriver.SubscribeAsync("messages", (ch, value) => {
           // *** Or any other access to a WiseJ object...
           var toast = new Toast(value);
           toast.Show();
       });
    }
  3. 
    

    I create a background service that takes care of the REDIS channels, in Startup.cs:

    using IHost host = Host.CreateDefaultBuilder(args)
        .ConfigureServices(services => {
           services.AddHostedService<RedisSubscriver>();
        }).Build();
    host.RunAsync();

    The event works, in fact when sending the message on the queue the debug stops at the code where the WiseJ Toast object is created (***).

    It is just not displayed in the main window.

Could you give me a hint or an example to study?
Thanks

  • You must to post comments
0
0

Hi Tiziano,

Thank you for your sample, there are a few things that I like to point out regarding how you’re using the background processes.

Any background service (or Hosted Service in this case) has its own separate thread, Wisej.NET’s session doesn’t exist in that thread’s scope, and since it’s not coming from the main application thread, nothing can be updated and/or pushed to the client.

You’ll have to go with another approach and see how it would update your application’s main thread.

You can follow Microsoft’s Documentation in that regard.

But the main point is, what’s happening is that when that breakpoint hits, it’s actually the service thread calling itself and since the Wisej.NET application isn’t scoped, there’s nothing to update!

It is not a Wisej.NET issue, even with a regular ASP.NET Core application this would yield the same result.

If you need additional help, we offer Professional Service packages that you can check out!

HTH,
Alaa

  • You must to post comments
0
0

Hi,
As requested, I have created a project containing only one subscriber to redis messages.

  • It is first necessary to have a working REDIS server, with Docker this is done with the following command:
       docker run -p 6379:6379 --name redis-master -e REDIS_REPLICATION_MODE=master -e ALLOW_EMPTY_PASSWORD=yes bitnami/redis:latest
  • This way locally at the specified port REDIS will run, then I installed redis-desktop-manager-0.9.3.817.exe
  • Start the project (possibly change the address of REDIS Server in the Setup.cs file)
  • Put a break point in the class “RedisSubscriver.cs” on line 20
  • Enter redis-desktop-manager and connect to the REDIS server give the connection a name (localhost:6379). Click on the connection name and press the REDIS command prompt icon.
  • Send a command to the ‘messages’ channel :
     PUBLISH messages "My Message"

It must display “1” and stop at the break point, in the variable “value” will be the message.

Proceeding however the “Toast” will not display the message

Thank you for your help

 

  • You must to post comments
0
0

Hi Tiziano,

Would you be able to wrap up a small sample for us to see what’s wrong?

We can’t just rely on code snippets, we need to have a small, runnable sample for us to quickly help you out!

Thank you,

Alaa

  • Tiziano Salardini
    Hi Alaa, I have left a small example project with instructions, as you requested, I hope it is clear. Thank you very much
  • You must to post comments
Showing 3 results
Your Answer

Please first to submit.