3

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?

markop
  • 186
  • 11

1 Answers1

3

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');
andrzej1_1
  • 1,151
  • 3
  • 21
  • 38
  • thanks. This almost does it. I would always put the subscribe functions in the onOpenFunctions array because they need to be reexecuted on every network drop and reconnect. So I would remove the `if` in the ´addOnOpenFunction` and always go with it's first block. – markop Mar 20 '17 at 23:56
  • I updated code in *addOnOpenFunction()*. Please accept answer if it is correct now. – andrzej1_1 Mar 21 '17 at 00:08
  • come to think about it, this should be included in autobahn-js as an option in the subscribe function. I'll file a feature request. – markop Mar 21 '17 at 08:20