Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unexplained arguments passed to JS client reconnectionStrategy

I’m working through some code examples for the Diffusion JS API but I don’t understand the example on reconnection. What are the start and abort parameters of the reconnectionStrategy?

// Create a reconnection strategy that applies an exponential back-off
var reconnectionStrategy = (function() {
    return function(start, abort) {
        var wait = Math.min(Math.pow(2, attempts++) * 100, maximumAttemptInterval);

        // Wait and then try to start the reconnection attempt
        setTimeout(start, wait);
    };
})();

// Connect to the server.
diffusion.connect({
    host : 'diffusion.example.com',
    port : 443,
    secure : true,
    principal : 'control',
    credentials : 'password',
    reconnect : {
        timeout : maximumTimeoutDuration,
        strategy : reconnectionStrategy
    }
}).then(function(session) {

Taken from https://github.com/pushtechnology/diffusion-examples/blob/master/js/examples/reconnect.js

like image 495
John Newby Avatar asked Jul 07 '16 13:07

John Newby


2 Answers

Those two arguments are described as reconnect and abort in the manual, both are functions that can be put to use.

  • start/reconnect - will initiate a reconnect attempt. Instructs the client to attempt another connection.

  • abort - may be called to abort reconnection, in which case the client will be closed. Call this if you believe further attempts will be fruitless or otherwise undesirable.

With this understanding we see that the example attempts reconnection between waits that increases exponentially (100ms, 200ms, 400ms, etc.) up to a maximum of 60s. If the reconnection attempt fails then the reconnection strategy function is called again.

like image 124
Martin Cowie Avatar answered Oct 31 '22 13:10

Martin Cowie


The function also doesn't need to be wrapped:

    var reconnectionStrategy = function(start, abort) {
    var wait = Math.min(Math.pow(2, attempts++) * 100, maximumAttemptInterval);

    // Wait and then try to start the reconnection attempt
    setTimeout(start, wait);};

would do just as well and be less confusing.

like image 4
Kat Avatar answered Oct 31 '22 12:10

Kat