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.