Making Cross-Domain Calls in SignalR

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.

About these ads

Tags: ,

8 Responses to “Making Cross-Domain Calls in SignalR”

  1. Matt Says:

    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

  2. akzent Says:

    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.

  3. Matt Says:

    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!

    • omairalam Says:

      I am new bie.
      Please send me the sample project of Making Cross-Domain Calls in SignalR
      My Email is
      omair64@hotmail.com

      • Matt Says:

        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

  4. Harnessing SignalR in SharePoint « SPMatt Says:

    [...] 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 [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: