title: Check if user is online in Firebase
body: <p>I found an example of what I need at Google Firebase documentation <a href="https://firebase.google.com/docs/database/web/offline-capabilities" rel="nofollow noreferrer">here</a>.</p>
  
  <p>Still, I want to modify it a bit to make it check the presence of user every second/10 seconds or at least every minute depending on how this will affect the load on the server so I came up with this: </p>
  
  <pre><code>TestApp.prototype.initFirebase = function() {
      this.database = firebase.database();
      this.database.ref(".info/connected").on("value", this.isOnline.bind(this));
  };
  
  TestApp.prototype.isOnline = function(snap) {
      var i=0;
      console.log(snap.val());
      setInterval(function() {
          if (snap.val() === true) {
              console.log("connected"+(i+=10));
          } else {
              console.log("not connected");
          }
      }, 10000);
  }
  </code></pre>
  
  <p>But here is what happens in the console if I run it: </p>
  
  <blockquote>
  <pre><code>main.js:34 false
  main.js:91 User signed out
  main.js:34 true
  main.js:39 not connected
  main.js:37 connected10
  main.js:39 not connected
  main.js:37 connected20
  main.js:39 not connected
  main.js:37 connected30
  main.js:39 not connected
  main.js:37 connected40
  </code></pre>
  </blockquote>
  
  <p>It triggers the function every 10 seconds but it shows me both results which are <code>connected</code> and <code>disconnected</code> at the same time. (actually, there is delay around 1 second) Moreover, it completely ignores if user is logged in or not and shows me the same logs every time. </p>
  
  <p>I want it to run <code>isOnline</code> method only if the user is logged in(with email and password). And if that is true <code>isOnline</code> should send request to the server if user is online every N seconds. Is there anything I can do? </p>
  
  <p><em>It would be much better if I could check if user is connected from the server side because I need to make some actions as long as user remains online. But I am not sure if that's possible so I think the best way is to check on frontend and trigger actions with HTTP triggers.</em> </p>