stomp-js / stomp-websocket

Stomp client for Web browsers and node.js apps
https://stomp-js.github.io/stomp-websocket/
Apache License 2.0
141 stars 36 forks source link

Reconnect fires only once #15

Closed lifecoderua closed 7 years ago

lifecoderua commented 7 years ago

Maybe I miss something but it looks like only one reconnect attempt is made. If it fails (server unavailable, etc.) setTimeout is fired and then client just waits forever. Any good way to work around this?

kum-deepak commented 7 years ago

This is tested with RabbitMQ, where it does not seem to have that problem. Which server are you using?

lifecoderua commented 7 years ago

I'm using STOMP over SockJS, this issue occure if the server is down,

I'm working on the fix right now, and don't want to stuck with my own fork of STOMP client. It looks like this issue may be worked around as a part of STOMP client itself (handling failed WS connection attempt), or as a wrapper for WS library. Which way works better for you?

I would like the retry attempt on the STOMP level as a general solution, but not sure it is possible to consistently track the different libs' errors. On the other hand SockJS is the go-to lib for STOMP, mentioned in the documentation and tutorials, which drives our backend team for this exact stack.

kum-deepak commented 7 years ago

Before I proceed with more suggestions - are you using the SockJS as suggested in https://stomp-js.github.io/stomp-websocket/codo/extra/docs-src/sockjs.md.html

Key is to pass a function that returns a SockJS object (so that before each connect/reconnect it can create a new SockJS instance).

In any case, please fork and submit a pull request. https://stomp-js.github.io/stomp-websocket/codo/extra/docs-src/Contribute.md.html may help you in getting started.

ermannos commented 7 years ago

I had the same issue, after the first disconnection the client was stuck with the message 'Opening Web Socket...'. After changing my code following kum-deepak suggestions everything work fine, the stomp client continues to try to reconnect. The key is to create sockjs client in a function, and to pass that function in constructor of the stomp client.

lifecoderua commented 7 years ago

Hm, function may be a way. I have a code from the original stomp-websocket, which works over a single object, which should be the reason. I've end up with a reconnecting-websocket library, connected to a SockJS plain websocket server (//websocket), which works just as good.

Thank you for the support!

zkendall commented 6 years ago

I am also having this problem. But I am not using SockJS. Why should reconnect depend on SockJS or the method the connection is created?

I've tried both: this.stomp = Stomp.over(() => { return new WebSocket(endpoint) }); this.stomp = Stomp.client(endpoint);

UPDATE: Nevermind... I didn't realize I had to set the reconnect_delay parameter. Works in both cases.

kum-deepak commented 6 years ago

Good to know.

A simple explanation of why reconnect might behave differently: SockJS and WebSocket are two different protocols. Internally SockJS has multiple variants for actual underlying connectivity. This make catching the disconnect event to behave differently. In some of the SockJS connectivity variants it may be a while before this library will know about the disconnection. Once this library knows that it has disconnected the reconnect process is same.