IntelliTect / IntelliTect.AspNetCore.SignalR.SqlServer

A Microsoft SQL Server backplane for ASP.NET Core SignalR.
Apache License 2.0
39 stars 5 forks source link

Is AddSqlServer all that is required? #4

Closed elylv closed 2 years ago

elylv commented 2 years ago

I have an existing Hub and would like to share the connections from 4 servers between one another, so a message sent from one server will get routed to the correct server that the relevant client is connected to.

The documentation seems quite sparse... do I really only have to add the .AddSqlServer() on the SignalR, and that's it? Do I need to do anything special OnConnectedAsync(), or IHubContext.Clients.Client(c).SendAsync(...) to ensure that I'm pulling in the SQL version of the clients? And if I do the SendAsync, it will automatically take care of sending from one of my other servers if the client is so connected?

It can't be that easy, right?

ascott18 commented 2 years ago

You don't need to do anything special that you don't need to do with any other SignalR backplane provider - which is to say, nothing aside from what's in the documentation. It really is that easy. This library provides the same services as the official MSFT-published Azure SignalR Service and Redis providers do, but for SQL Server.

When sending a message, that message is pushed out to the backplane (SQL Server), and from that point each of your 4 connected servers will see that message and determine if it needs to be passed along to any of its connected clients. This is short-circuited when sending to a single client who is already connected to the server the message is being sent by.

elylv commented 2 years ago

Awesome! Thanks for the quick reply. I guess I still don't understand the mechanism for the other servers picking up when they should send a message, but I guess that's what your code does, so I will trust in the magic!

ascott18 commented 2 years ago

@elylv When you send a message from your hub in C#, that message is inserted into a SQL Server table. Your other servers are constantly looking at that table for new rows (either by SELECTing every few seconds, or ideally via SQL Server Service Broker which lets SQL Server send a push notification to your server when a new row comes in).

As each server observes new rows in SQL Server (again, representing SignalR messages), that server then relays that message along to any clients connected to that server who are the appropriate audience for that message (checking for specific connection, group, or all depending on which proxy you sent it with from Hub.Clients - this information about who the message is for is also part of the row for that message in SQL Server).

elylv commented 2 years ago

Very nice. I had thought of a similar workaround to the problem myself, but would have to have scheduled jobs running to poll, and it would be a big mess. This is much easier.