22

I'm working with an application that is written in Node.js and Express, and I'm trying to use the Stomp.js client to connect to an ActiveMQ server.

I can get the application to connect to ActiveMQ just fine using Stomp, but I am unable to get the system to automatically reconnect upon connection failure. It seems like the failure function is only called if the connection is initially successful and then later severed, though if ActiveMQ is already down when the Node app starts, I do see the error message that proves the failure function was called.

var Stomp = require('stompjs');
var stompClient = Stomp.overTCP('localhost', 61612);
var stompStatus = false;

var stompSuccessCallback = function (frame) {
    stompStatus = true;
    console.log('STOMP: Connection successful');
};

var stompFailureCallback = function (error) {
    stompStatus = false;
    console.log('STOMP: ' + error);

    setTimeout(stompConnect, 10000);
    console.log('STOMP: Reconecting in 10 seconds');
};

function stompConnect() {
    console.log('STOMP: Attempting connection');
    stompClient.connect('login', 'password', stompSuccessCallback, stompFailureCallback);

}

stompConnect();

Does anybody have any idea what's going on here?

Michael Oryl
  • 20,856
  • 14
  • 77
  • 117

2 Answers2

50

The WebSocket that is held by the Stomp.client can only be opened once. If there is a network failure, reconnecting with the same StompClient will not work as the web socket will remain closed.

This can definitely be improved by stomp.js but in the mean time, you can workaround this by recreating a Stomp.client when a failure is detected. Something like:

var stompClient;

var stompFailureCallback = function (error) {
    console.log('STOMP: ' + error);
    setTimeout(stompConnect, 10000);
    console.log('STOMP: Reconecting in 10 seconds');
};

function stompConnect() {
    console.log('STOMP: Attempting connection');
    // recreate the stompClient to use a new WebSocket
    stompClient = Stomp.overTCP('localhost', 61612);
    stompClient.connect('login', 'password', stompSuccessCallback, stompFailureCallback);
}
jmesnil
  • 696
  • 6
  • 3
  • 1
    Thanks very much, Jeff. I really appreciate you taking the time to come out here and answer this for me. – Michael Oryl Mar 14 '14 at 18:41
  • 1
    I used the same concept but when it says "whoops! connection was lost" in the middle the FailureCallback does not gets called. – Vivek Sadh May 19 '16 at 12:43
  • Hi Jeff ( @jmesnil ), I am using Stomp.js client in my application. I tried your way of handling a connection lost scenario. Although it created a connection again yet I am not able to subscribe to the events anymore. Doing so throws the error: "WebSocket is already in CLOSING or CLOSED state". Please help. – Lalit Jun 14 '16 at 14:36
  • It seems you don't re-create stompClient in your code Kumar, or it gets cached somehow. Make sure you're using latest version of stomp.js BTW. – mvmn Aug 08 '17 at 13:04
2

The original sompjs is no longer maintained. Please use https://github.com/stomp-js/stomp-websocket This version has support for automatic re-connection. On each successful connection, the connect callback is called where you can do your subscribes.

If you are using Angular 2, 4, or 5. You should look at https://github.com/stomp-js/ng2-stompjs This package not only support automatic re-connection, but it will also re-subscribe all queues and send any pending messages.

Deepak Kumar
  • 944
  • 9
  • 12