SignalR 2.2 and ARR3.0 Load balancing
Asked Answered
S

2

3

I am using signalr2.2 web application and hosted in IIS-8. Here I tried to implement load balancing for my application using ARR3.0. In my server farm I connected two servers and set the routing rule as 'weighted round robin'. Every server has a javascript client with HTML page. While I am accessing HTML page of any server the request has been routed as expected but I am having this error in my html page and signalr doesn't get connected

WebSocket connection to 'ws://mydomain.com/signalr/connect?transport=webSockets&clientProtocol=1.5&connectionToken=x9rAfatyMm3TbqzrYKL1K37Z4tKrUTH7bwNmSItW55Z0ms6pe43YiQGFOmaHMyT%2BpmGz62ukt4tha72vPcJhdqLtIvsQvqvY15oYGv69JjzSuuiSL7v2l%2FccZT6tQa3Z&connectionData=%5B%7B%22name%22%3A%22chathub%22%7D%5D&tid=1' failed: Error during WebSocket handshake: Unexpected response code: 400

If I make offline anyone of this server in my serverfarm, signalr worked fine and no errors will thrown. Help me to find the solution.

If I communicate signalr server from my .net client application it will throw the following error

Error during start request. Stopping the connection.

Susurrous answered 29/7, 2016 at 4:43 Comment(2)
You need to setup the same machine key on all the servers in the farmWendish
Hi @janithat000, I need to clarify, In server farm after implemented machine key changes, Whenever I hit the url, onconnected happened in one server and hub method call happened in another server. Is this the procedure in routing? or am I missing anything ? pls help me. Note : I am using pass through authentication in application nodeSusurrous
D
6

I had this problem as well. Looking deeper, the error is: The connection id is in the incorrect format. After googling a bit, I read somewhere that when running a farm environment, the servers need to share the same machine key, which can be configured in IIS.

To this, follow these steps:

  • Open the IIS connecting to any of your servers in the farm.
  • In the server node, open the Machine Key option in the right pane
  • In the right menu, click Generate Keys
  • Uncheck the Generate a unique key for each application option for both Validation key and Decryption key.
  • Copy the two keys generated (e.g paste in notepad)
  • Make sure all servers in the farm share this same keys (the ARR server doesn't need this), you might need to repeat the steps above in them (maybe you don't as soon as they use Shared Configuration).

Also, you need to make sure your application in each server use the same Identity. For example, select your application node (or site node if it's the case), in right menu select Basic Settings, then Connect as.... If the selected option is Application user, then you need to check how is your Application Pool configured. If it uses Network Service (as were mine), you'll still have problems, because each server uses its own credentials, but you have to make sure they use the same Identity.

In my scenario, I had to create a user with access to every server in the farm, and make the application use this same user. Only after doing this my problem was solved.

Dolley answered 18/4, 2017 at 18:17 Comment(6)
Thanks Alisson. I will try this and let u knowSusurrous
Hi @Alisson , It is working fine and I never get any socket error after this. Thanks for this info. But one thing I need to clarify, In server farm after implemented machine key changes, Whenever I hit the url, onconnected happened in one server and hub method call happened in another server. Is this the procedure in routing? or am I missing anything ? pls help me. Note : I am using pass through authentication in application nodeSusurrous
Any idea about my comment ?Susurrous
@Susurrous oh, I forgot about that, sorry. In the default configuration, IIS distributes each request to the servers equally. So first request goes to server A, next request to server B, next to A again, and so on. When you connect to SignalR, actually IIS is receiving a request, and the connection will be forwarded to one of the servers. When you call a hub method, it's normal that it goes to the other server. If you call it again, it should go to the next server, and so on. So what's happening to you is the expected behaviour.Dolley
Every time I tried round robin load balance I got this script warning '[Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check xhr.spec.whatwg.org.' do have idea about this ?Susurrous
@Susurrous take a read here, it might help you.Dolley
S
2

In addition to @Alisson's answer I found an easy way for setting Machine Key.

You can directly do this setting in your Web.config file. Simple create an entry for machineKey under system.web section. The entry will look like

<system.web>   
    <machineKey validationKey="###############YOUR#####VALIDATION ######KEY####################################################################################" decryptionKey="#####YOUR####DECRYPTION#####KEY#################" validation="SHA1" decryption="Auto" />
</system.web>

A unique Validation key and Decryption key can be generated by steps given in @Alisson's answer.

By using this approach you don't need to worry about your server change at any point or deployment at any other web farm server. Your code will run without any issues.

Somatotype answered 27/6, 2018 at 11:5 Comment(2)
Do you know, if i need to 'Uncheck the Generate a unique key for each application option for both Validation key and Decryption key', as explained above? Or would it be enough, to add your configuration into the config file?Broke
Yes, it's enough to add the configuration in config file.Somatotype

© 2022 - 2024 — McMap. All rights reserved.