Life cycle events

With hubs, there is a full set of events you can subscribe to, to deal with the life cycle.

The events you have are as follows:

connectionSlow
reconnecting
reconnected
disconnected

All of these events can be useful, not only for your application but also for notifying the user as to what is going on. This is especially true for slow connections and disconnected:

$.connection.hub.connectionSlow(function() {
    // Notify user about slow connection
});
$.connection.hub.disconnected(function() {
    // Notify user about being disconnected
});

For disconnected connections, you might want to retry connecting after a certain amount of time out; for instance, after 5 seconds:

$.connection.hub.disconnected(function() { setTimeout(function() { $.connection.hub.start(); ...

Get SignalR - Real-time Application Development - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.