daniel daniel - 2 months ago 37
Javascript Question

Signalr check if hub already started

I have multiple javascript blocks with signalR functions.

I don't know the order of execution so that i want to start the hub with

$.connection.hub.start();


if it isn't started already.

How can i check if the hub is already started? Starting it multiple times it it throws an error.

Answer

You can detect when the hub has started using .done()

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

using this method, you can do the following (Taken from docs : https://github.com/SignalR/SignalR/wiki/SignalR-JS-Client-Hubs) you can then keep track of if the connection is open yourself.

function connectionReady() {
    alert("Done calling first hub serverside-function");
};

$.connection.hub.start()
                .done(function() {
                    myHub.server.SomeFunction(SomeParam) //e.g. a login or init
                         .done(connectionReady); 
                })
                .fail(function() {
                    alert("Could not Connect!");
                 });