Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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());
        }
   });
like image 906
Vivek Sadh Avatar asked Jan 29 '26 22:01

Vivek Sadh


1 Answers

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

like image 134
Vivek Sadh Avatar answered Jan 31 '26 18:01

Vivek Sadh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!