Quantcast
Channel: Active questions tagged blazor - Stack Overflow
Viewing all articles
Browse latest Browse all 4839

Subscribe to Redis Channel with Blazor Server

$
0
0

In .NET 7, Blazor Server, using https://stackexchange.github.io/StackExchange.Redis/Basics

I'm trying to figure out how to subscribe to a Redis channel, receive constant updates, pass those updates along to all listening components. I have a subscription set up, a middle-man service to pass the data to (which works), and a listener in a component to try to get constant updates. The component isn't getting hit after the RedisService.Invoke() gets called. Any help on the pattern is appreciated.

I attempted to wire up the ConnectionMultiplexer in App.razor, however, it doesn't continue to receive updates of the subscription listener when I navigate away from the home page.

Therefore, in Program.cs, I did the following:

 ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(builder.Configuration.GetSection("RedisSettings").GetValue<string>("server")); ISubscriber sub = redis.GetSubscriber(); sub.Subscribe(builder.Configuration.GetSection("RedisSettings").GetValue<string>("channel")).OnMessage(async channelMessage => {     await Task.Delay(1000);     if (!string.IsNullOrEmpty(channelMessage.Message))     {         var redis = builder.Services.BuildServiceProvider().CreateScope().ServiceProvider.GetRequiredService<RedisService>();         redis.Set(channelMessage.Message);     } });

My RedisService is simple. // SET DOES get hit multiple times as I publish new data into the channel.

public string Message { get; private set; }public event Action OnMessageChanged;public void Set(string message){    Message = message;    OnMessageChanged?.Invoke();}

In a component:

@inject RedisService _redisprotected override async Task OnInitializedAsync(){    await base.OnInitializedAsync();    _redis.OnMessageChanged += HandleMessageChanged;}void HandleMessageChanged() // THIS DOESN'T GET HIT{}

Viewing all articles
Browse latest Browse all 4839

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>