Search code examples
wampcrossbarautobahnjs

reestablish WAMP subscriptions after reconnect


I'm using autobahn-js (0.11.2) in a web browser and the crossbar message router (v17.2.1) in the backend.

In case of a network disconnect (e.g. due to poor network) the autobahn-js client can be configured to try to reconnect periodically.

Now in my web app powered by autobahn subscriptions to different WAMP topics are created session.subscribe('my.topic', myhandleevent) dynamically.

Is there a best practice on how to reregister all active subscriptions upon reconnect? Is that maybe configurable even?


Solution

  • I think resubscriptions are not configurable out-of-box. But onopen is fired after reconnect, so placing subscriptions initialization inside it, will do the thing:

    var ses;
    var onOpenFunctions = [];
    
    function addOnOpenFunction(name) {
        onOpenFunctions.push(name);
        if (ses !== null) {
            window[name]();
        }
    }
    
    connection.onopen = function (session, details) {
        ses = session;
        for (var i = 0; i < onOpenFunctions.length; i++) {
            window[onOpenFunctions[i]]();
        }
    };
    

    Then if you want subscribe dynamically you have to do this:

    function subscribeTopic() {
        session.subscribe('my.topic', myhandleevent)
    }
    addOnOpenFunction('subscribeTopic');