angular-signalr-hub
angular-signalr-hub copied to clipboard
Add auto-reconnect feature
How to use the reconnect feature?
Hey @saucysalamandar, there isn't a "reconnect" feature built into this wrapper, however you should be able to listen for state changes using the stateChange
option and react accordingly when a hub disconnects. I haven't messed much with reconnection, but it looks like it some cases its better to just dispose the connection and start a new one:
http://stackoverflow.com/questions/23375043/best-practice-for-reconnecting-signalr-2-0-net-client-to-server-hub
Hello,
I also need to make the hub to reconnect automatically. I think this lib need some major improvement to manage this.
Obviously, what we need is:
- when we disconnect, we try to reconnect to the hub every X seconds. In this example, I put 5.
- new hub return a promise which is resolved when the connection is OK. This is often used in order to subscribe to a specific chan (for example you want to send a signal to a specific group of people). Unfortunately, a reconnection cannot trigger this again.
To proceed, you can add into the lib before the line "return Hub;" the following code:
Hub.connection.disconnected(function () {
setTimeout(function () {
Hub.connection.start();
}, 5000); // Restart connection after 5 seconds.
});
then if like me you have to do some code when you are connected (like subscribing to a specific chan), you can do it under stateChanged property:
stateChanged: (state) => {
if (state.newState === $.signalR.connectionState.connected) {
console.log("connected");
this.myHub.Subscribe(this.lessonId);
}
if (state.newState === $.signalR.connectionState.disconnected) {
console.log("disconnected");
}
}
Without changing a lot of things, I think an option to activate the autoreconnection can be nice to add into this lib.
I agree, automatic reconnect would be helpful.
Not sure if it makes sense to do through configuration (e.g reconnectTimeout: 5000) or callback handler.
It's been a while since anyone commented on this. I'm flagging this with enhancement
. If you're interested in this feature, please upvote or comment