angular-signalr-hub icon indicating copy to clipboard operation
angular-signalr-hub copied to clipboard

Add auto-reconnect feature

Open myo-ko opened this issue 8 years ago • 4 comments

How to use the reconnect feature?

myo-ko avatar Jun 06 '16 08:06 myo-ko

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

JustMaier avatar Sep 30 '16 19:09 JustMaier

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.

Adavo avatar May 23 '17 09:05 Adavo

I agree, automatic reconnect would be helpful.

Not sure if it makes sense to do through configuration (e.g reconnectTimeout: 5000) or callback handler.

brownrw8 avatar Oct 25 '17 17:10 brownrw8

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

JustMaier avatar Jan 03 '18 19:01 JustMaier