Search code examples
javawebsocketstomp

jquery stomp websockets server reconnection reinitialization


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?


Solution

  • 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.