1
votes

I am trying to run my first SignalR v2 project but with no luck, $.connection is undefined.

Here is the error from web console:

  1. Uncaught TypeError: Cannot read property 'chatHub' of undefined (anonymous function)
  2. k
  3. l.fireWith
  4. p.extend.ready
  5. D

My hub:

using Microsoft.AspNet.SignalR;

namespace InstantMessage
{
    public class ChatHub : Hub
    {
        public void Hello()
        {
            Clients.All.hello();
        }
    }
}

Startup.cs

using Microsoft.Owin;
using Owin;

[assembly: OwinStartup(typeof(InstantMessage.Startup))]
namespace InstantMessage
{
    public class Startup
    {
        public void Configuration(IAppBuilder app)
        {
            app.MapSignalR();
        }
    }
}

Fontend:

<head>
    .....
    <!--Script references. -->
    <!--Reference the jQuery library. -->
    <script src="~/Scripts/jquery-1.8.2.min.js"></script>
    <!--Reference the SignalR library. -->
   <script src="~/Scripts/jquery.signalR-2.0.0.js"></script>
    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>
</head>
<body>
<h2>Instant Message Demo</h2>

<div class="container">
        <input type="text" id="message" />
        <input type="button" id="sendmessage" value="Send" />
        <input type="hidden" id="displayname" />
        <ul id="discussion">
        </ul>
    </div>


    <!--Add script to update the page and send messages.--> 
    <script type="text/javascript">
        $(function () {
            // Declare a proxy to reference the hub. 
            console.log($.connection);

            var chat = $.connection.chatHub;
            // Create a function that the hub can call to broadcast messages.
            chat.client.broadcastMessage = function (name, message) {
                // Html encode display name and message. 
                var encodedName = $('<div />').text(name).html();
                var encodedMsg = $('<div />').text(message).html();
                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + encodedName
                    + '</strong>:&nbsp;&nbsp;' + encodedMsg + '</li>');
            };
            // Get the user name and store it to prepend to messages.
            $('#displayname').val(prompt('Enter your name:', ''));
            // Set initial focus to message input box.  
            $('#message').focus();
            // Start the connection.
            $.connection.hub.start().done(function () {
                $('#sendmessage').click(function () {
                    // Call the Send method on the hub. 
                    chat.server.send($('#displayname').val(), $('#message').val());
                    // Clear text box and reset focus for next comment. 
                    $('#message').val('').focus();
                });
            });
        });
    </script>

</body>

It seems like the js code in /signalr/hubs is correct, chathub is there and the autogeneration of the file works fine.

$.hubConnection.prototype.createHubProxies = function () {
        var proxies = {};
        this.starting(function () {
            // Register the hub proxies as subscribed
            // (instance, shouldSubscribe)
            registerHubProxies(proxies, true);

            this._registerSubscribedHubs();
        }).disconnected(function () {
            // Unsubscribe all hub proxies when we "disconnect".  This is to ensure that we do not re-add functional call backs.
            // (instance, shouldSubscribe)
            registerHubProxies(proxies, false);
        });

        proxies.chatHub = this.createHubProxy('chatHub'); 
        proxies.chatHub.client = { };
        proxies.chatHub.server = {
            hello: function () {
                return proxies.chatHub.invoke.apply(proxies.chatHub, $.merge(["Hello"], $.makeArray(arguments)));
             }
        };

        return proxies;
    };

Should also mention that I installed Signalr from nuget and I am using VS2012.

3
Are your script includes in the right order?davidfowl
Set a debug breakpoint on app.MapSignalR() to see that is actually generating the "/signalr/hubs" pathGustavo Armenta
Yes they are all included, look like this: <script src="~/Scripts/jquery-1.8.2.min.js"></script> <script src="~/Scripts/jquery.signalR-2.0.0.js"></script> <script src="~/signalr/hubs"></script> Yes, when I add a break point on app.MapSignalR() I hit it on application start. But still it refuse to work..Mattias Hagström
@MattiasHagström my problem seems exactly same as yours. how did you fix it?Amit Mishra

3 Answers

5
votes

Removing BundleConfig.RegisterBundles(BundleTable.Bundles); from Global.asax.cs fixed this for me. I encountered this problem because jQuery was being included twice.

1
votes

Place a metatag like the following just before your ChatHub Class definition

[HubName("chatHub")]

0
votes

Is your reference to jquery correct? The SignalR package currently installs 1.6.4--