Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set Meteor Reconnect Time

A client of my meteor app attempts to reconnect to the server in increasing time intervals. Using ((Meteor.status().retryTime - (new Date()).getTime())/1000).toFixed(0), I have roughly estimated that the reconnect intervals are 1st: 1 second , 2nd: 2 seconds, 3rd: 4 seconds , 4th: 12 seconds, 5th: 18 seconds, 6th: 62 seconds, 7th: 108 seconds. Is there a way to set the interval length? For example, could I set the reconnect interval to 5 seconds every time no matter how many times I have already tried to reconnect?

like image 803
Nate Avatar asked Feb 11 '23 17:02

Nate


1 Answers


UPDATE: I built a package to achieve this functionally - nspangler:autoreconnect


My final solution was to track the Meteor.status() and build a custom interval when the status was waiting. This is code on the client.

 // Variable For Storing Interval ID
 var intervalId = null;

 Meteor.startup( function () {

  // Interval Reconnect
  Tracker.autorun( function () {
    // Start Pinging For Recconect On Interval, only if status is faiting and intervalId is null
    if(Meteor.status().status === "waiting" && intervalId === null) {
        intervalId = Meteor.setInterval( function () {
            console.log("attempt to reconnect");
            Meteor.reconnect()
        }, 1000);
        console.log(intervalId);
    }
    // Stop Trying to Reconnect If Connected, and clear Interval
    if(Meteor.status().status === "connected" && intervalId != null) {
        console.log("cleared interval");
        Meteor.clearInterval(intervalId);
        intervalId = null;
    }
  })

});
like image 83
Nate Avatar answered Feb 13 '23 07:02

Nate