stomp-js / rx-stomp

STOMP adaptor for RxJS
Apache License 2.0
111 stars 21 forks source link

RXStomp, check max attempt and exponential reconnectdelay #462

Closed shipra0278 closed 2 years ago

shipra0278 commented 2 years ago

I have requirement in Angular to use websocket to get real time updates for the data grid from Active MQ. I used Websocket to do this and need to subscribe to "topics". I followed https://stomp-js.github.io/guide/rx-stomp/rx-stomp-with-angular.html and created ConfigurationPermalink RxStompServicePermalink Factory to create and initialize RxStompServicePermalink

Everything works fine. Requirment: Now we need to check on the max attempt(4) to reconnect and reset the reconnect delay for every attempt.

I coded in the rxStompServiceFactory. as below, please guide me the following code is correct or is there any better way?

export function rxStompServiceFactory() {

  let maxConnectionAttempts = 4;
  let currentTry = 0;

  const rxStomp = new RxStompService();

  myRxStompConfig.webSocketFactory = function () {
    return new SockJS('http://localhost:8080/');
  };

  myRxStompConfig.beforeConnect = () => {
    currentTry++;

if(currentTry> 1)
    {
      console.log(`Connection Time:  (${Date.now.toString()})
                  ReconnectDelay:   (${myRxStompConfig.reconnectDelay}) `)
        myRxStompConfig.reconnectDelay = 500 * Math.pow(2, currentTry);
        console.log(`Exponential back off - next connection attempt in ${myRxStompConfig.reconnectDelay}ms`);
    }
    if (currentTry > maxConnectionAttempts) {
      console.log(`Exceeds max attempts (${maxConnectionAttempts}), will not try to connect now`);
      // It is valid to call deactivate from beforeConnect
      rxStomp.deactivate();
    }
  };

  rxStomp.configure(myRxStompConfig);

  if(currentTry !== maxConnectionAttempts)
     rxStomp.activate();

  return rxStomp;
}
kum-deepak commented 2 years ago

Updating myRxStompConfig in beforeConnect does not alter the rxStomp. Please try changing

   myRxStompConfig.reconnectDelay = 500 * Math.pow(2, currentTry);

to

   rxStomp.configure({reconnectDelay: 500 * Math.pow(2, currentTry)});

In addition, you should reset currentTry on a successful connection.

shipra0278 commented 2 years ago

Ok, thanks.

Also, when I configure the WebSocket with URL "ws://amq.net:30007/ws", I get error. but if I if set the brokerURL with same URL "ws://amq.net:30007/ws" in config file. It was fine.

myRxStompConfig.webSocketFactory = function () { return new WebSocket("ws://amq.net:30007/ws"); //return new SockJS(//'http://localhost:7777'); };

ERROR: Thu Aug 18 2022 08:26:06 GMT-0500 (Central Daylight Time) 'Opening Web Socket...' rx-stomp-service-factory.ts:15 WebSocket connection to 'ws://amq.........net:30007/ws' failed: _config_rx_stomp_config__WEBPACK_IMPORTED_MODULE_1__.myRxStompConfig.webSocketFactory @ rx-stomp-service-factory.ts:15 _createWebSocket @ client.js:278 (anonymous) @ client.js:211 fulfilled @ client.js:4 invoke @ zone.js:372

rx-stomp.config.ts:29 Thu Aug 18 2022 08:26:06 GMT-0500 (Central Daylight Time) 'Connection closed to undefined' rx-stomp.config.ts:29 Thu Aug 18 2022 08:26:06 GMT-0500 (Central Daylight Time) 'STOMP: scheduling reconnection in 8000ms'

Thanks Shipra

kum-deepak commented 2 years ago

Glad to know it worked. I will close this issue.