Firebase presence system: never stops writing
<p><strong>Edit</strong> To clarify: I have two issues.
1. Why is my node overwritten?
2. Why is the callback apparently being called recursivly?</p>

<p>I follow <a href="https://firebase.google.com/docs/database/web/offline-capabilities#section-sample" rel="nofollow noreferrer">this guide</a> to implement a presence system to my webapp. I want all logged in users to always be able to know if all the other users are online or not.
Here is my code:</p>

<pre><code>  currentGameRef = gamesInProgressRef.child(newState.currentTable);
  var myConnectionsRef = currentGameRef.child("player" + newState.myPlayerNumber).child("connections");
  var lastOnlineRef = currentGameRef.child("player" + newState.myPlayerNumber).child("lastOnline");

  var connectedRef = firebase.database().ref('.info/connected');
  connectedRef.on("value", function(snap) {
    if(snap.val() == true){
      var con = myConnectionsRef.push(true);
      con.onDisconnect().remove();

      lastOnlineRef.onDisconnect().set(firebase.database.ServerValue.TIMESTAMP);
    }
  });
</code></pre>

<p>When I test the code, it writes thousands of <code>true</code> to each of the players "connections"-node. The wierd thing is that all the other children of each player is erased. Why is this happening?
A player node looks like this:</p>

<pre><code>"player1" : {
  "name": "Vladimir Putin",
  "country": "Russia",
  ...,
  ...,
  "connections" : {
    ...
    ...
  }
}
</code></pre>

<p>I don't set the "player1"-node in my code, just a child of it. I know for a 100 % that it is the above code that causes this behaviour, but I cant figure out why. Can someone please tell me why the code above clears the player nodes?</p>