Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Implement a reconnect option #16

Open
mschipperheyn opened this issue Jun 9, 2016 · 8 comments
Open

Implement a reconnect option #16

mschipperheyn opened this issue Jun 9, 2016 · 8 comments

Comments

@mschipperheyn
Copy link

There is no really great way to reconnect available it seems.

I'm currently implementing a timeout which recreates the client after a failure. That just seems like a very poor approach. stomp should have some kind of autoreconnect.

@yeyu456
Copy link
Contributor

yeyu456 commented Jun 10, 2016

Yep.I Agree.For now I try to do it by myself.You can refer to this reconnect websocket implement .

@JSteunou
Copy link
Owner

@mschipperheyn I actually do the same, but I dont know if it's should really be part of this lib. Pleased everyone on this feature would mean having a ton of options like delay, maxReconnectAttempt, ... things like that. Maybe this should be more like a plugin or wrapper around the stomp-client. The stomp-client is more just a JS implementation of STOMP protocol over websocket, dedicated to frames encode / decode.

@mschipperheyn
Copy link
Author

Thanks for the reference @yeyu456

@bfwg
Copy link

bfwg commented Sep 13, 2016

My implementation, try to reconnect every 1 second after disconnected.

let ws = new WebSocket(socketUrl);
let client = webstomp.over(ws);
client.connect({}, (frame) => {
  successCallback();
}, () => {
  reconnect(socketUrl, successCallback);
});

function reconnect(socketUrl, successCallback) {
  let connected = false;
  let reconInv = setInterval(() => {
    ws = new WebSocket(socketUrl);
    client = webstomp.over(ws);
    client.connect({}, (frame) => {
      clearInterval(reconInv);
      connected = true;
      successCallback();
    }, () => {
      if (connected) {
        reconnect(socketUrl, successCallback);
      }
    });
  }, 1000);
}

@pdeva
Copy link

pdeva commented Sep 18, 2016

@bfwg in your error handler, you need to add connected = false otherwise reconnect will be called exponential times since more than 1 error can be thrown from a connection.

@bfwg
Copy link

bfwg commented Sep 18, 2016

@pdeva I have updated the implementation thanks to @teaegg

function connectAndReconnect(socketUrl, successCallback) {
    ws = new WebSocket(socketUrl);
    client = webstomp.over(ws);
    client.connect({}, (frame) => {
    successCallback();
  }, () => {
    setTimeout(() => {
      this.connectAndReconnect(socketUrl, successCallback);
    }, 1000)
  });
}

connectAndReconnect('ws//:xxxx:4321', () => { })

Looks good now?

@mschipperheyn
Copy link
Author

There are better ways to do this I think. I adapted some reconnection library I found in the wild that has a kind of "dying" algorithm to not keep pinging needlessly.

@ipluser
Copy link

ipluser commented Mar 1, 2019

This is a good issue, as @JSteunou said "stomp-client is more just a JS implementation of STOMP protocol over WebSocket", that sounds like rights.

So we can wrap WebSocket using some library like @yeyu456 reconnecting-WebSocket.

But, what should the stomp-client do? the stomp-client also need to reconnect.

I think we can provide a good way to reconnect the stomp-client, e.g.

The WebSocket Wrapper can provide a hook method that gives another can watch reconnect event.

// the socket provide a `reconnect` hook method to add listen reconnect event
// if socket reconnect, then invoke the listeners
const socket = WebSocketReconnecter('ws://....')

// if stomp-client find the `socket` has hook method, then stomp-client add a listener to do reconnect inside the code
stompClient = webstomp.over(socket, {
  reconnect: 'reconnect'  // String (name for hook method) or Function
})

stompClient.connect({}, () => {
  // todo
}, () => {
  // todo
})

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants