Callback on client does not get triggered with SignalR

1.3k Views Asked by At

I'm currently getting up to speed with SignalR, and tried to build a very basic message notification system using the very basic understanding of SignalR that I have but I can't get the messages to come back after the submission.

I've read up on numerous blogs on the topic and the very basic is to have a VOID method that accepts a string value and then passes that back by attaching it to the Clients context, however, this does not work for, as onReceiveNotification() is always undefined in JS debugger?

Am I missing something

notificationhub.cs

[HubName("notificationHub")]
public class NotificationHub : Hub
{

    public override System.Threading.Tasks.Task OnConnected()
    {
        return base.OnConnected();
    }

    public override System.Threading.Tasks.Task OnDisconnected(bool stopCalled)
    {
        return base.OnDisconnected(stopCalled);
    }

    public override System.Threading.Tasks.Task OnReconnected()
    {
        return base.OnReconnected();
    }

    [HubMethodName("sendNotifications")]
    public void SendNotifications(string message)
    {
        Clients.All.onRecieveNotification(string.Format("{0} {1}", message, DateTime.Now.ToString()));
    }
}

message.html page

<div>
    <input type="text" id="messageinput" />
    <button id="notifybutton">Send Message</button>
</div>

<script src="Scripts/jquery-2.1.4.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="signalr/hubs"></script>
<script type="text/javascript">
    jQuery(document).ready(function ($)
    {
        var $hub = $.connection.notificationHub;
        $.connection.hub.start();

        $(document).on("click", "#notifybutton", {}, function (e)
        {
            e.preventDefault();
            var $message = $("#messageinput").val();
            $hub.server.sendNotifications($message);
        });
    });
</script>

display.html

<div>
    <ul id="messages"></ul>
</div>

<script src="Scripts/jquery-2.1.4.min.js"></script>
<script src="Scripts/jquery.signalR-2.2.0.min.js"></script>
<script src="signalr/hubs"></script>
<script type="text/javascript">
    jQuery(document).ready(function ($)
    {
        var $hub = $.connection.notificationHub;

        $hub.onRecieveNotification = function (message)
        {
            $("#message").append($("<li></li>", { text: message }));
        }

        $.connection.hub.start();
    });
</script>
1

There are 1 best solutions below

0
On BEST ANSWER

It looks like you are missing client in:

   $hub.client.onRecieveNotification = function (message)
    {
        $("#message").append($("<li></li>", { text: message }));
    }