Automatic reconnect with Stomp.js in Node.js application

后端 未结 2 1017
半阙折子戏
半阙折子戏 2020-12-08 16:25

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 a

相关标签:
2条回答
  • 2020-12-08 16:33

    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.

    0 讨论(0)
  • 2020-12-08 16:46

    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);
    }
    
    0 讨论(0)
提交回复
热议问题