$.hubConnection is not a function(…)

14,692

I am not sure whether your version of jQuery is supported. I had strange side effects with the wrong version of jQuery being loaded or it being loaded multiple times.
When working with JSPM jspm resolve --only npm:[email protected] helps.

These verisons work:

<script src="jquery-2.1.4.min.js"></script>
<script src="jquery.signalR-2.2.0.min.js"></script>

and

"jquery": "npm:[email protected]",
"ms-signalr-client": "npm:[email protected]",

I also recommend to omit the "/signalr" postfix when specifying the hubConnection. It is added in automatically.

Share:
14,692
Admin
Author by

Admin

Updated on July 13, 2022

Comments

  • Admin
    Admin almost 2 years

    I ran into a problem with my SignalR project. I have created an console to run the SignalR and then trying to use it with my website (all running in localhost mode for now)

    SignalRSelfHost

    using System;
    using Microsoft.AspNet.SignalR;
    using Microsoft.Owin.Hosting;
    using Owin;
    using Microsoft.Owin.Cors;
    using Microsoft.AspNet.SignalR.Hubs;
    
    namespace SignalRSelfHost
    {
        class Program
        {
            static void Main(string[] args)
            {
                // This will *ONLY* bind to localhost, if you want to bind to all addresses
                // use http://*:8080 to bind to all addresses. 
                // See http://msdn.microsoft.com/en-us/library/system.net.httplistener.aspx 
                // for more information.
                string url = "http://localhost:8080";
                using (WebApp.Start(url))
                {
                    Console.WriteLine("Server running on {0}", url);
                    Console.ReadLine();
                }
            }
        }
        class Startup
        {
            public void Configuration(IAppBuilder app)
            {
                app.UseCors(CorsOptions.AllowAll);
                app.MapSignalR();
            }
        }
        [HubName("myHub")]
        public class MyHub : Hub
        {
            public void Send(string name, string message)
            {
                Clients.All.addMessage(name, message);
            }
        }
    }
    

    Index.Html

    <script src="Scripts/jquery-3.1.1.min.js"></script>
    
    <script src="Scripts/jquery.signalR-2.2.1.min.js"></script>
    
    <script src="http://localhost:8080/signalr/hubs"></script>
    
    <script type="text/javascript">
        $(function () {
            //Set the hubs URL for the connection
            //$.connection.hub.url = "http://localhost:8080/signalr";
    
            //// Declare a proxy to reference the hub.
            //var chat = $.connection.myHub;
    
            var conn = $.hubConnection("http://localhost:8080/signalr");
            var hubProxy = conn.createHubProxy('myHub');
    
    
            conn.start().done(function () {
            });
        });
    </script>
    

    Some of the above has been outcomented, because that is something i also tried but didnt work either.

    Can anyone tell me why i get the error: $.hubConnection is not a function(…)

  • Mukus
    Mukus over 7 years
    how can you remove the url when your sever and client are on different servers?
  • Kelso Sharp
    Kelso Sharp over 7 years
    Sorry it took so long to answer you, the proxy is automatically generated, the it knows how to resolve it. What you are doing is trying to tell it to resolve at a specific location, Which isn't necessary.
  • Kelso Sharp
    Kelso Sharp about 7 years
    /Sarcasm on/ I love when people mark down an answer without providing a reason. /Sarcasm off/ No way to know if I am actually wrong (Which would not be the first time nor the last), or if they misunderstand the answer. Simply marking an answer down helps no one.