Getting SignalR to talk to a hub on another domain

Posted by on Sep 30, 2013 in JavaScript, SignalR

I recently met the challenge of getting SignalR to talk to a hub on a different domain to the one it was running on the client. Finding working and up-to-date information on how to do this is a challenge given the rapid release cycle of the SignalR library. This raises all kinds of security concerns as the same-origin request issue comes-to-light. However, it can be done using the following configuration.

In the project file, edit the Global.asax file as follows:

This instructs the SignalR ‘server’ component (i.e. the hub) to accept requests from domains other than that the site is currently configured on. Without this the request will be blocked.

That’s the server side taken care of, but CORS is a major client-side security concern (although not much these days given the prevalence of client-side JavaScript services).

On the client, the proxy used to communicate with the server needs to be re-directed to the new host. If you don’t do this, signalR will try and initiate a hub connection on the current domain, so will make a request to

/signalr/negotiate/?_=123456789

Which will be on the same host/domain as the site is running, not the alternate domain intended to be targeted.
Therefore you will need to set the URL for the client connection as follows:

note: the window.location.protocol just ensures the connection retains the HTTP/HTTPS setting

then, when starting the hub, be sure to tell it that it will be connecting cross-domain:

Once you’ve done the above, you should be good to go.