How do I get a SignalR hub connection to work cross-domain?

I am trying to get a super simple hub connection working cross-domain but having no luck. I’ve read dozens of posts and done everything mentioned but still no success.

My server hub is here

public class ChatHub : Hub
    public void Send(string name, string message)
        Clients.All.broadcastMessage(name, message);

My server MapHubs call is here

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

Any my javascript client is here

<!DOCTYPE html>

    <meta name="viewport" content="width=device-width" />
    <script src=""></script>
    <script src="~/Scripts/jquery.signalR-1.1.2.min.js"></script>
    <script src=""></script>
    <div class="container">
        <input type="text" id="displayname" value="Test" />
        <input type="text" id="message" value="I'm here" />
        <input type="button" id="sendmessage" value="Send" />
    <script type="text/javascript">
        $(function ()
            $.connection.hub.url="http://<my url>/";
            var chat = $.connection.chatHub;
            $.connection.hub.start().done(function ()
                alert("Connection succeeded");
            }).fail(function ()
                alert("Connection failed");

The problem is that it never reaches the Connection succeeded or failed alerts and the alert(chat) call returns undefined.

I’ve tried several combinations for the $.connection.hub.url line

$.connection.hub.url="http://<My url>";
$.connection.hub.url="http://<My url>/";
$.connection.hub.url="http://<My url>/signalr";
$.connection.hub.url="http://<My url>/signalr/";

The developer console in Chrome and Firebug give me the error

Uncaught Error: SignalR: Error loading hubs. Ensure your hubs reference is correct, e.g. <script src=""></script>. 

On the same domain it works fine. This is really starting to drive me crazy so any help would be appreciated.


Your server is being hosted cross domain yet you’re trying to get the hubs from the current domain. Therefore it’s failing to retrieve the hubs file and you don’t actually have a proxy to work with (which is why everything is not working).

So you have two options:

  1. Manually create the hubs file and host it on the current domain:
  2. Use the raw hub connection API and do not include the signalr/hubs file at all.
Read More:   Javascript: how to dynamically create nested objects using object names given by an array

Here’s a code snippet of how you can use the raw hub connection API: (second code snippet).

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Similar Posts