reestablish WAMP subscriptions after reconnect

434 Views Asked by At

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?

1

There are 1 best solutions below

3
On BEST ANSWER

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');