25

Using persistent connections and an ASP.NET JavaScript client, I am trying to connect to a sub-domain not the same as the one the page was served from.

ASP.Net Page from webserver sub1.mydomain.com wants to connect to SignalR at sub2.mydomain.com. The same code works fine when connecting within the same sub-domain.

I found another post where cross-domain connections were enabled with:

jQuery.support.cors = true;

but this did not work for me.

How can I connect to SignalR in a second sub-domain using persistent connection and a JavaScript client?

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
codezoo
  • 593
  • 1
  • 6
  • 16

6 Answers6

38

You need to do one of the following to make it work:

  • Set up $.connection.hub.url = 'http://subdomain.domain.com/signalr';, pointing to your subdomain.
  • Enable cross domain on the server:

    RouteTable.Routes.MapHubs(new HubConfiguration()
    {
      EnableCrossDomain = true
    });
    
Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Piotr Szmyd
  • 13,371
  • 6
  • 44
  • 61
  • 1
    I've tried the first and third bullet, but I am using persistent connection and so the second bullet would not apply. Still fails to connect. – codezoo Apr 03 '12 at 23:42
  • @codezoo For persistent connections you can also specify a url, eg. `var con = $.connection('echo'); con.url = http://my.domain.com/echo.` Specifying the full URL here: `$.connection('http://my.domain.com/echo')` should also work, although I haven't checked. – Piotr Szmyd Apr 04 '12 at 05:29
  • Thanks Piotr, I am currently using this method: $.connection('http://my.domain.com/echo') – codezoo Apr 04 '12 at 10:58
  • I should note that when you're self-hosting signalR, the `Server` appears to add the Access-Control-Allow-Origin header automatically. – Ken Smith Nov 16 '12 at 19:09
  • @PiotrSzmyd setting the url through `$.connection('http://my.domain.com/echo')` was not enough to start the hub cross-origin. When the hub starts it gets the client _host_ when `hub.url` is not set with a host. – Josiah Ruddell Apr 09 '13 at 23:08
  • 3
    I get this output: ...\Global.asax.cs(17,64,17,81): error CS0117: 'Microsoft.AspNet.SignalR.HubConfiguration' does not contain a definition for 'EnableCrossDomain' ...\Global.asax.cs(17,13,17,91): error CS0619: 'System.Web.Routing.SignalRRouteExtensions.MapHubs(System.Web.Routing.RouteCollection, Microsoft.AspNet.SignalR.HubConfiguration)' is obsolete: 'Use IAppBuilder.MapSignalR in an Owin Startup class. See http://go.microsoft.com/fwlink/?LinkId=320578 for more details.' – Akira Yamamoto Feb 03 '14 at 14:20
  • @AkiraYamamoto The way to do Cross Domain communication in SignalR 2.0 is to add an OWIN Startup class, take the `IAppBuilder` object and call `app.MapHubs(new HubConfiguration { EnableJSONP = true });` – joe_coolish Feb 20 '14 at 16:00
  • 1
    But I still would like to use WebSockets when available, not JSONP. – Akira Yamamoto Feb 20 '14 at 17:16
  • For SignalR 2, [Ian Mercer's answer](//stackoverflow.com/a/35067160/2747593) is correct (and doesn't require enabling JSONP). – Scott Weldon Nov 05 '16 at 00:52
13

In the current version of SignalR, using the now separate CORS package, the API for this has changed to:

public void Configuration(IAppBuilder app)
{
    app.Map("/signalr", map =>
    {
        map.UseCors(CorsOptions.AllowAll);
        var hubConfiguration = new HubConfiguration
        {
        };
        map.RunSignalR(hubConfiguration);
    });
}

See ASP.NET SignalR Hubs API Guide - JavaScript Client.

Scott Weldon
  • 9,673
  • 6
  • 48
  • 67
Ian Mercer
  • 38,490
  • 8
  • 97
  • 133
  • 5
    You'll also need to add `using Microsoft.Owin.Cors;` (as well as install the corresponding NuGet package) if you haven't already. – Scott Weldon Nov 05 '16 at 00:46
  • Is it safe to use AllowAll? Wouldn't this mean any website could be set up to receive notifications from SignalR? – David Klempfner Jun 23 '21 at 23:20
7

If switching from 0.5.1 to 0.5.2, you may have had the following:

$.connection.hub.start({ transport: 'longPolling', xdomain: true }, function () {...

Which can be changed to:

$.connection.hub.start({ jsonp: true }, function () {...
ElHaix
  • 12,846
  • 27
  • 115
  • 203
4

In Signalr2, you can use the pre-release of Microsoft.Owin.Cors, currently 3.0.0-rc1 as of writing this: Microsoft.Owin.Cors.

More information can be found here:

anAgent
  • 2,550
  • 24
  • 34
1
var connection = $.connection('http://somecrossdomainurl/echo')
connection.start({ transport: 'longPolling', xdomain: true });

https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client

jlp
  • 9,800
  • 16
  • 53
  • 74
1

What really have solved my issue was:

1 - Add this to global.asax:

RouteTable.Routes.MapHubs(new HubConfiguration() { 
    EnableCrossDomain = true 
});

2- Set up the web.config of my web project to enable the cross-domain:

<system.webServer>
    <httpProtocol>
        <customHeaders>
            <add name="Access-Control-Allow-Origin" value="*" />
            <add name="Access-Control-Allow-Headers" value="Content-Type" />
        </customHeaders>
    </httpProtocol>
</system.webServer>

3- Then I change the implementation on my html, changing the $.connection() to as follow:

var connection = $.hubConnection('http://localhost.my:8081/signalr');
var chatHubProxy = connection.createHubProxy('chatHub');

Versions I'm using:

  • signalR-1.1.0

  • jquery.signalR-1.1.3.js

  • dot.net 4.0

Claudio Santos
  • 1,307
  • 13
  • 22
  • I am using the 4 sub domains , should i have to host my hub class on each sub domains or should i have to host my complete website on all the subdomain including the web pages that i have? – G.S Bhangal Sep 11 '14 at 06:04
  • 1
    RouteTable.Routes.MapHubs(config); is deprecated; CORS is now enabled by default per http://weblogs.asp.net/davidfowler/signalr-0-5-1-released – Ben H Mar 31 '15 at 03:03