What is the best way to monitor app connection status?


#1

I wanted to add a simple connection checker similar to what you see on atmosphere and other meteor sites. If the connection breaks in the app a simple toast is displayed, upon reconnect it goes away. Is there a reactive method already in place for this? Currently I’m checking Meteor.status().connected in a function that’s called by starting a setInterval.

Is this necessary? It works fine I just wanted to see if this was the proper way of handling this.


#2

I do this to make it reactive. In my case, I change the color of a signal indicator icon in the app’s top navbar. Session changes, the color changes.

if(Meteor.isClient){
Meteor.autorun(function () {
    var stat;
    if (Meteor.status().status === "connected") {
        stat = 'lime'
    }
    else if (Meteor.status().status === "connecting") {
        stat = 'yellow'
    }
    else {
        stat = 'red';
    }
    Session.set('status',stat);
});
}

#3

I render a different template based on Meteor.status().connected since it is already a reactive datasource

Template[getTemplate('connectionstatus')].helpers({
	connected: function () {
		return Meteor.status().connected;
	}
});

#4

Good suggestions! I knew there was a more native way of handling this. Thanks!


#5

Hi,

With the help of this code, I actualy do this :

Meteor.autorun(function () {
        if (Meteor.status().status === "connected") {
            console.log("connected");
            $scope.closeToast();
        }
        else if (Meteor.status().status === "connecting") {
            console.log("reconnecting");
        }
        else {
            console.log("disconnected");
            $scope.showToastWarning('No connection');

        }

    });

But, each time the autorun run it does it 7 times. Any idea to make it run only one time ?

Thanks in advance


#6

Looks to me like you’re using angular-meteor. You should change things around to destroy the autorun when the scope destroys (even if it doesn’t). And you’re also calling Meteor.status() multiple times and should be assigned to a variable instead.

$scope.$meteorAutorun(function() {
  var status = Meteor.status().status;
  if (status === "connected") {
    console.log("connected");
    $scope.closeToast();
  } else if (status === "connecting") {
    console.log("reconnecting");
  } else {
    console.log("disconnected");
    $scope.showToastWarning("No connection");
  }
});

#7

@svenskunganka thanks a lot for your help. It is all i needed !

juste great !