SB2055
SB2055

Reputation: 12862

Detecting when signalR hub is ready after hub.start()

I have a parent and child viewModel. The parent viewmodel starts the signalR connection:

$.connection.hub.start()

The child viewmodel - one that loads only when the user accesses chat - does the following:

chat.server.addUserToChat(self.currentUsername()).done(function() {
    alert('added');
});

The problem is, the child call is happening before the parent call. I can fix this with a setTimeOut of 1 second, but ideally I could do something like this:

$.connection.hub.ready(function(){chat.server.addUserToChat(self.currentUsername()).done(function() {
        alert('added');
    });});

Is there anything like this in signalR? Or do I need to use timeouts / pub/sub between viewmodels?

Upvotes: 0

Views: 3634

Answers (2)

Lars Höppner
Lars Höppner

Reputation: 18402

This seems to be more an issue of structuring your application. You can store the deferred object returned by hub.start() in some global object and access it in your child viewmodel:

window.chatApp = {
    hubConnector: $.connection.hub.start()
};

// in your child viewmodel
chatApp.hubConnector.done(function () {
    chat.server.addUserToChat(self.currentUsername()).done(function () {
        alert('added');
    });
});

Upvotes: 4

Geezer68
Geezer68

Reputation: 391

Try this

$.connection.hub.start().done(function () {
  // hub is now ready
  chat.server.addUserToChat(self.currentUsername()).done(function() {
    alert('added');
  });
});

Upvotes: 0

Related Questions