Search code examples
websocketactivemq-classicstomp

Reconnect stomp when disconnected


I am using the following code to create/subscribe to a topic and handle the messages. Sometimes the connection gets lost and the error says:

Whoops! The connection was lost...

I would like to know if there is a way to reconnect it. Is it possible in the error callback or define the whole code in a method and call it recursively in error callback ?

 $(document).ready(function () {
  ........
  ...............
      try {
            var socket = new SockJS("${createLink(uri: '/stomp')}");
            var client = Stomp.over(socket);
            client.connect({}, function () {
            client.subscribe("/topic/${userInstance?.username}",                 
            function (message) {
           ............
           ....................

              });
            });
        } catch (error) {
            console.log("ERROR: " + error.toString());
        }
   });

Solution

  • I managed to do it using failure callback and connect again. It will keep trying as long as it will fail.