javascriptfirebaseuser-presence

firebase presence becomes more and more wrong over time


I have a simple presence user-count set up for firebase based on their example. The problem is that it relies on removing counts on disconnect. However, firebase seems to go down every 2 months and removes the ondisconnect handlers. This means that over time the counts get more and more wrong. Is there any way to fix this?

ty.Presence = function() {
  this.rooms = {}
  this.presence = fb.child('presence')
  this.connectedRef = fb.child('.info/connected');
  if (!localStorage.fb_presence_id) {
    localStorage.fb_presence_id = Math.random().toString(36).slice(2)
  }
  this.browserID = localStorage.fb_presence_id
  var first = false   
}


ty.Presence.prototype.add = function(roomID, userobj) {
  var self = this
  var userListRef = this.presence.child(roomID)

  // Generate a reference to a new location for my user with push.
  var obj = {
    s: "on",
    id: this.browserID
  }
  if (userobj) {
    obj.u = {
      _id: userobj._id,
      n: userobj.username
    }
    if (userobj.a) {
      obj.u.a = userobj.a
    }
  }

  var myUserRef = userListRef.push(obj)
  this.rooms[roomID] = myUserRef
  this.connectedRef.on("value", function(isOnline) {
    if (isOnline.val()) {
      // If we lose our internet connection, we want ourselves removed from the list.
      myUserRef.onDisconnect().remove();
    }
  });
};

ty.Presence.prototype.count = function(roomID, cb) {
  var self = this
  var userListRef = this.presence.child(roomID)
  var count = 0

  function res () {
    var usersArr = _.pluck(users, 'id')
    usersArr = _.uniq(usersArr)
    count = usersArr.length
    if (cb) cb(count)
  }

  var users = {}

  userListRef.on("child_added", function(css) {
    users[css.name()] = css.val();
    res()
  });

  userListRef.on("child_removed", function(css) {
    delete users[css.name()]
    res()
  });

  cb(count)
};

ty.Presence.prototype.get = function(ref) {
  return this[ref]
};

ty.Presence.prototype.setGlobal = function(object) {
  var self = this

  _.each(this.rooms, function (myUserRef) {
    myUserRef.set(object)
  })
};

ty.Presence.prototype.remove = function(roomID) {
  if (this.rooms[roomID])
    this.rooms[roomID].remove();
};

ty.Presence.prototype.off = function(roomID) {
  var userListRef = this.presence.child(roomID)
  userListRef.off()
};


ty.presence = new ty.Presence()
ty.presence.add('all')

Solution

  • The onDisconnect handlers can be lost if a Firebase is restarted (e.g. when a new release is pushed live). One simple approach is to attach a timestamp as a priority to the records when they are stored. As long as the client remains online, have him update the timestamp occasionally.

    setInterval(function() {
        connectedRef.setPriority(Date.now());   
    }, 1000*60*60*4 /* every 4 hours */ );
    

    Thus, any record which reaches, say, 24 hours old, would obviously be an orphan. A challenge could take place by clients (e.g. when a new client receives the list for the first time) or by a server process (e.g. a node.js script with a setInterval() to check for records older than X).

    presenceRef.endAt(Date.now()-24*60*60*1000 /* 24 hours ago */).remove();
    

    Less than ideal, sure, but a functional workaround I've utilized in apps.