Josh B
Josh B

Reputation: 65

Call SignalR Client Method on Startup

I want to call a client side method from my SignalR hub class only once, when the client first loads up the webpage. How would I go about doing this?

Upvotes: 2

Views: 552

Answers (1)

Mark C.
Mark C.

Reputation: 6450

In your BlahBlahHub class, you have access to all of the connection methods that you do on the Client. Hint: Look at the base Hub class.

With that being said, here's what the code would look like:

Hub

[HubName("messageHub")]
    public class MessageHub : Hub
    {
        public override Task OnConnected()
        {
            Clients.Caller.alertUser(88);

            return base.OnConnected();
        }
    }

Client

var sender = $.connection.messageHub;

$.connection.hub.start().done(function () {

}).fail(function (reason) {
    console.log("SignalR connection failed: " + reason);
});

sender.client.alertUser = function (test) {
    alert(test);
};

Upvotes: 3

Related Questions