Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Redis with SignalR

Tags:

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.

like image 552
labyrinth Avatar asked Mar 22 '16 17:03

labyrinth


People also ask

Does SignalR use Redis?

The SignalR Redis backplane uses the pub/sub feature to forward messages to other servers. For this tutorial, you will use three servers: Two servers running Windows, which you will use to deploy a SignalR application. One server running Linux, which you will use to run Redis.

What is a Redis backplane?

Redis backplane acts like a middleman between the two sites and notifies all subscribed sites with any changes. In this blog, I will demonstrate how to use Signalr with Redis to create a simple chat application.

Is SignalR scalable?

A SignalR app can scale out based on the number of messages sent, while the Azure SignalR Service scales to handle any number of connections.

How does backplane SignalR work?

The backplanes work by replacing the default IMessageBus with a bus designed for that backplane. For example, the message bus for Redis is RedisMessageBus, and it uses the Redis pub/sub mechanism to send and receive messages. Each server instance connects to the backplane through the bus.


1 Answers

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

like image 173
Erkan Demirel Avatar answered Oct 02 '22 13:10

Erkan Demirel