I am currently trying to use SignalR in a .NET web app. After reviewing numerous posts/articles, I'm still stumped. On page load, I am receiving an error:
Uncaught TypeError: Cannot read property 'client' of undefined
My current implementation is as follows:
BundleConfig.cs:
//note jquery is rendered in a shared bundle in _layout.cshtml. I have
//verified in Chrome debugger it is loaded before the bundles shown here
bundles.Add(new ScriptBundle("~/bundles/signalr").Include(
"~/Scripts/jquery.signalR-2.2.3.js"));
bundles.Add(new ScriptBundle("~/bundles/home")
.Include("~/Scripts/Home/Index.js"));
Bundles rendered in view:
@section scripts {
@Scripts.Render("~/bundles/signalr")
@Scripts.Render("~/bundles/home")
}
Startup.cs:
public void Configuration(IAppBuilder app)
{
app.MapSignalR();
}
DashboardHub.cs:
namespace Sample.SignalR
{
[HubName("dashboardHub")]
public class DashboardHub : Hub
{
public void sendMessage(string message)
{
Clients.All.receiveMessage(notes);
}
}
}
Index.js:
$(function () {
var messageHub = $.connection.dashboardHub;
//send message to hub so hub updates other clients
$(document).on("submit", "#messageForm", function () {
messageHub.server.sendMessage($("#Message").val());
});
//receive message
messageHub.client.receiveMessage= function (message) {
$("#Message").empty();
$("#Message").text(notes);
}
$.connection.hub.start();
});
The error is being thrown on page load in the script on line:
messageHub.client.receiveNotesUpdate = function (notes) {
Some things I've checked and double checked:
- scripts are rendered on the page in the correct order
- hub names and method names match between client and server (have tried with
and without the
[HubName("dashboardHub")]
annotation) - hub class is public
I'm not sure why messageHub
is undefined at the point. How can I resolve this?