Learning SignalR: Unable to get property 'client' of undefined or null reference

One of the most common exceptions you will hit when you first started with a SignalR chat program could look like the following:

Unhandled exception at line 78, column 13 in https://localhost:53632/home/chat

0x800a138f - JavaScript runtime error: Unable to get property 'client' of undefined or null reference

Why?

This is mostly like caused by the fact that your Hub class name and your javascript probably does not match. It is case sensitive. By default, SignalR runtime uses camel casing of the Hub class name.

For example, if your hub class looks like the following: 

    1: public class MyChatHub : Hub 
    2: { 
    3:     public void Send(string name, string message) 
    4:     { 
    5:         Clients.All.broadcastMyMessage(name, message); 
    6:     } 
    7: }

And the proxy referenced in your HtmlPage1.html might look like the following:

    1: // Declare a proxy to reference the hub.  
    2: var chat = $.connection.MyChatHub;

Now this does not work since camel casing means that you need to use "myChatHub" instead. so to fix it, you can modify your client side page to use the correct casing.

    1: // Declare a proxy to reference the hub.  
    2: var chat = $.connection.myChatHub;

Now if you would like to override this default behavior, you could add the HubName attribute to your MyChatHub class. SignalR runtime will use your name as it is, in whatever casing you like, as the name of the proxy.

    1: [HubName("MyChatHub")] 
    2: public class MyChatHub : Hub 
    3: { 
    4:     // omit the method here for simplicity 
    5: }

 

Hope this helps.

Comments

  • Anonymous
    May 08, 2013
    Try adding reference like below to your class attribute [Microsoft.AspNet.SignalR.Hubs.HubName("MyChatHub")]

  • Anonymous
    May 08, 2013
    The comment has been removed

  • Anonymous
    July 24, 2013
    Currently bowing down in praise of this post for fixing what promised to be a day wasting problem

  • Anonymous
    January 01, 2014
    Thanks Man u saved my day. it worked

  • Anonymous
    June 24, 2014
    it is nice,I like it. http://www.shareplz.net/