Using Redis with SignalR
Asked Answered
R

2

23

I have an ASP.NET MVC application that runs on server A and some web services that run on server B. I have implemented real-time notifications for which I have used SignalR on server A. But now I need server B to also be able to send messages to a View served from server A (the main web application). Hence, I am trying the tutorial here to involve Redis backplane.

In my startup in server A, I have added the following:

GlobalHost.DependencyResolver.UseRedis("localhost", 6379, string.Empty, "abc");
app.MapHubs();

Here, I assume that "myApp" indicates the channel and when I run publish abc "hello world" on the Redis console, I can see the subscriber count returned as 1, but I am not able to figure out how a SignalR hub interacts with the channel. Where do I receive the message on the server/view? Can we subscribe to only one redis channel? Can't we dynamically configure to subscribe to a particular channel?

EDIT: I can see messages sent from chat Application implemented using SignalR on redis console if I subscribe to abc.

Also for now I have implemented my own redis listener on server A which in receiving a message from redis channel, calls the signalR hub function. I am sure there must be a different way to do this and I am hoping redis backplane can help me but unsure how it works.

Rodolphe answered 22/3, 2016 at 17:44 Comment(3)
One simple solution. Publish a message on desired channel on Server B. Subscribe the channel on Server A and push the message to signalR hub manually.Roddie
This may sound stupid but it would help me understand with clarity. Does this mean that every message to view can be sent only through server A and never through server B directly? What is the use of signalR.redis here then?Rodolphe
Please check this :- gigi.nullneuron.net/gigilabs/… Also Below too:- asp.net/signalr/overview/performance/scaleout-with-redisOutcry
B
51

Backplane distributes messages between servers.

GlobalHost.DependencyResolver.UseRedis("localhost", 6379, string.Empty, "abc");

Here, abc is the redis channel, that means whichever server is connected to redis server with this channel, they will share messages. SignalR channel (group) is different than Redis channel. You can share also SignalR channel (group) messages.

Then just install the Microsoft.AspNet.SignalR.Redis NuGet to your servers.

Connect your servers to Redis like this:

 GlobalHost.DependencyResolver.UseRedis("server", port, "password", "AppName");
 app.MapSignalR();

Then, use your signalr as before. You don't have to do anything else.

When Server A sends a message to the clients, it will send the message first to Redis. Then Redis will share the message with all subscribers (servers A and B). Then, A and B will send the message to their clients. (Also viceversa is true, it will be same for if B sends a message).

Let's say A sends a message to the clients. _context.Clients.All.TestMessage("Hello");

This will go first to redis and redis will share this with A and B.

Then both A an B will send this message to their clients.

_context.Clients.All.TestMessage("Hello");

But you don't have to worry about these kind of things. I said before. Install package, conntect your servers to redis and use signalr as before.

If we come in your question. The answer is Yes. Server B can send messages to server A clients by Signalr Backplane.

This image summarizes what I told:

enter image description here

Brunn answered 25/3, 2016 at 7:54 Comment(3)
Thanks for an elaborate explanation! Can I say here then that we cannot dynamically subscribe/unsubscribe to redis channels and instead use global channel in servers A and B and use all messages on these channels?Rodolphe
First of all Redis Channel and Signalr Channel are different thing. Secondly, you can't change redis channel dynamically (why you want change?) after mapping signalr . Signalr channels(groups) you can use dynamically. If Server B Sends message a group(channel), Server A clients who is connected this group(channel) will get this message.Brunn
You can ask if you have question and accept this as an answer, if it has answered your questionsBrunn
H
1

Just as a 2023 update, at this moment the Microsoft.AspNet.SignalR.Redis package is using a deprecated redis nuget package (strong name). At this moment, the latest package would be Microsoft.AspNet.SignalR.StackExchangeRedis and instead of:

UseRedis(...)

you have

UseStackExchangeRedis(...)

Harpist answered 1/3, 2023 at 21:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.