1

I have a websocket app that connects to my Java backend via stomp.js file.

function connect() {

            var socket = new SockJS('<?php echo $rootbasename;?>wsconnect');
            stompClient = Stomp.over(socket);
            stompClient.debug = null;
            stompClient.connect({}, function (frame) {
                console.log('Connected: ' + frame);
                console.log('/queue/messages/' + widgetId + "/" + $.cookie(cookiename));
                stompClient.subscribe('/queue/messages/' + widgetId + "/" + $.cookie(cookiename), function (result) {
                    //code here

                }, {userToken: $.cookie(cookiename), widgetId: widgetId});

                stompClient.subscribe('/queue/makereadresult/' + widgetId + '/' + $.cookie(cookiename), function (result) {

});

But, what if my java backend server will reboot? I want customers not to notice any change. Is there a way to auto-reconnect on connection lost? Or any way to make it smooth for clients?

avalon
  • 2,231
  • 3
  • 24
  • 49

1 Answers1

2

When you are connecting you can pass in error callback. In there you could have reconnect logic. For example stomp has this method

client.connect(login, passcode, connectCallback, errorCallback);

and in errorCallback just call connectCallback.

Janar
  • 2,623
  • 1
  • 22
  • 32
  • Thanks, but the issue is error callback fires just once, but what if my server down? For 5 minutes. I need it to auto-reconnect when server is up again, but your approach is not working in this case. – avalon Jul 04 '17 at 06:19
  • 1
    you can write whatever you want in the error callback. you could use setInterval method to try to get connection and keep it going till it connects – Janar Jul 04 '17 at 07:16
  • Ok thanks. But your answer worked with chain with this thread's accepted answer: https://stackoverflow.com/questions/22361917/automatic-reconnect-with-stomp-js-in-node-js-application – avalon Jul 04 '17 at 08:42