SignalR OnConnected firing on different server to the one which it's actually connected

150 Views Asked by At

I'm using SignalR and a web farm in IIS, currently with 3 servers and requests are load balanced via ARR.

There are certain external events that happen which I want to be processed by the server to which the client is connected. So I want to track which of the 3 servers the client is currently connected.

I thought that I could do this using OnConnected and within that method store the MachineName against the ConnectionID in redis. The problem is that OnConnected seems to get called an a different server to the one that the client is connected to.

Upon investigating, it seems that there are three calls, one to /negiotate one to /connect and one to /start. The /connect seems to be the websocket connection that is kept up for the duration, the others are just transient. These three connections can happen on different servers, and it seems that the websocket connection can be to server A (so that's the server that the client's SignalR connection is going to), but the OnConnected gets fired on server B.

I was wondering if I'm overlooking something that will let me see which server the SignalR connection is actually connected to?

Thanks,

Will

2

There are 2 best solutions below

2
Kelso Sharp On

If you are going to use a web farm, then you need to implement a backplane to track all of the messaging.

https://learn.microsoft.com/en-us/aspnet/signalr/overview/performance/scaleout-in-signalr

Without a proper backplane implementation its impossible to do what you want to do.

6
Frank M On

I believe that is something you would have to save. Assuming you are using a database for mapping users, you could have an additional field such as "LoggedInOn" and store the server host name or other identifier.

However, other than some aspect of troubleshooting your are looking to do, proper send/receive of messages should cross the backplane to all servers. This way no matter which server they are connected to, messages are received.

If you have external events as you say, once they complete and a message is ready to be sent back to a client, the backplane should push that to all servers.

If that's not happening I would review the docs as Kelso Sharp stated.