I saw this stack overflow post today on how to use SignalR across domains:
http://stackoverflow.com/questions/9751157/signalr-accross-domains
Since I wanted to try out SignalR anyway I thought it would be a nice way to get started and see if I can solve the problem and write a small tutorial on how to do it.
The first step in using SignalR cross-domain is to tell it where to find the hub to connect to. This is done using the following code that you should insert at the beginning of your JavaScript code:
$.connection.hub.url = 'http://[someotherdomain]/signalr';
If you try your application like this it will not work and you will have the same problem as the poster mentioned above. This is because for security reasons many browsers don’t allow cross-domain calls by default. A server has to add the special Access-Control-Allow-Origin
header to its response to tell the browser to allow cross domain calls to this specific site.
You can add this value to your ASP.NET page using:
Response.AppendHeader("Access-Control-Allow-Origin", "*");
However even with this modification SignalR won’t do the cross domain call – at least not in Internet Explorer 9 where I tested it. Like the original poster I was stuck for quite a while because I didn’t find any solutions. The error message I got was:
SignalR: Connection must be started before data can be sent. Call .start() before .send()
Poking arround the jQuery code and debugging a bit I eventually saw that there is a check inside jQuery to see if the call is a cross domain call or not and eventually this part of the code will terminate with the error “No transport”.
With this new error message I came upon this page explaining the problem and suggesting a possible solution to force jQuery into making a cross domain call using the following JavaScript line:
jQuery.support.cors = true; //force cross-site scripting
And it worked!!!
I also found some references suggesting JSONP as a possible solution, but it seems more complex and at least for me the solution above works fine.
Tags: SignalR cross-domain ASP.NET JavaScript jQuery, stack overflow
April 9, 2012 at 1:30 pm |
Hi Thomas
Nice summary of the main pain points you’ll hit with cross-domain calls with SignalR. Curious to know however if you got this working in the latest versions of FireFox and Chrome? I notice you mentioned you tested in IE9.
Problem being, i’ve followed this example and a number of others (only difference being the Access-Control-Allow-Origin being set in the web.config) and only managed to get it working in IE9. FireFox and Chrome fail to work – Chrome offers the most informative error pointing to cross-domain calls being the issue:
XMLHttpRequest cannot load http://localhost:2284/signalr/negotiate. Origin http://localhost is not allowed by Access-Control-Allow-Origin.
Would be interested to know if you had your example working in all browsers, and if so, would be very keen to see it in action.
Cheers
April 9, 2012 at 4:47 pm |
Hey Matt,
thanks for your feedback! I tested my solution in Firefox 3.6.3, Chrome 18 and Internet Explorer 9 and it’s working in all of them. Make sure that the headers are really sent to the client (developer console) and that you are using the newest jQuery versions (I’m using 1.6.4). If the clients send special headers you might need to send Access-Control-Allow-Headers (careful: no * allowed as value). Also try setting Access-Control-Allow-Methods. Could it be a caching problem?
If you want I can send you my sample project and you can see if you can get it working.
May 28, 2012 at 5:30 am |
Hi budy,
Could u please mail me the sample project of
Making Cross-Domain Calls in SignalR
my email is
omair64@hotmail.com
Thanks
May 28, 2012 at 7:46 am
I send you the sample project. It is based on the post from Scott Hanselman with cross domain support added like described in the post: http://www.hanselman.com/blog/AsynchronousScalableWebApplicationsWithRealtimePersistentLongrunningConnectionsWithSignalR.aspx
April 10, 2012 at 8:37 am |
Thanks for your quick reply!
A colleague of mine managed to get it working – turns out what we needed was the latest DLLs and scripts, and to include { transport: ‘longPolling’, xdomain: true } in the send call. May not be the same as your solution but it works so I’m happy with that!
May 28, 2012 at 5:25 am |
I am new bie.
Please send me the sample project of Making Cross-Domain Calls in SignalR
My Email is
omair64@hotmail.com
May 28, 2012 at 5:56 am
Hi Omair
Everything I did to get the example working is on my blog in the pingback below. My example is specific to SharePoint so unless that’s the environment you’re working in then there’s not much point sending the solution (I don’t have it available right now anyway) – you should be able to get what you need from the post though.
Good luck
April 12, 2012 at 9:19 am |
[…] here. One page I did come across which got me most of the way there was Thomas Krause’s Making Cross-Domain Calls in SignalR which summed it up pretty nicely, but still didn’t work in all browsers for me. But more on […]
February 19, 2015 at 9:45 pm |
The way to do CORS has completely changed now, and you shouldn’t do it in javascript.
http://www.asp.net/signalr/overview/guide-to-the-api/hubs-api-guide-javascript-client#crossdomain
December 19, 2017 at 8:10 am |
Very good, thanks.