How to tell when a Stomp server disconnected from the Stomp.JS client

Oops just figured it out I will delete in a bit if it doesn't help others. I needed to use SockJS client instead of the Stomp one...

var socket = new SockJS('/hello');
...
socket.onclose = function() {
    console.log('close');
    stompClient.disconnect();
    setConnected();
};

As pointed out by muttonUp stomp.js from https://github.com/jmesnil/stomp-websocket/ will overwrite the onclose handler. On the other hand it provides the option to pass an error-callback on connect:

stompClient.connect({}, function(frame) {
    ...
}, function(message) {
    // check message for disconnect
});

Since you will get several kinds of errors delivered to your callback, you have to check the message it it was indeed the "Whoops! [...]" which indicates a connection loss.

Tags:

Stomp

Sockjs