title: firebase return onSnapshot promise
body: <p>I'm using firebase/firestore and I'm looking a way to return promise of snapshot.</p>
  <pre><code>onlineUsers(){
       // i want to return onSnapshot
      return this.status_database_ref.where('state','==','online').onSnapshot();
  }
  </code></pre>
  <p>in other file I did</p>
  <pre><code>  componentDidMount(){
      // this.unsubscribe = this.ref.where('state','==','online').onSnapshot(this.onCollectionUpdate) 
      firebaseService.onlineUsers().then(e=&gt;{
          console.log(e)
      })
  }
  </code></pre>
  <p>I get the errors</p>
  <blockquote>
  <p>Error: Query.onSnapshot failed: Called with invalid arguments.</p>
  <p>TypeError: _firebaseService2.default.unsubscribe is not a function</p>
  </blockquote>
  <p>if i do this way</p>
  <pre><code>onlineUsers(){
     return  this.status_database_ref.where('state','==','online').onSnapshot((querySnapshot)=&gt;{
          return querySnapshot
      }) 
  }
  </code></pre>
  <p>I get</p>
  <pre><code>TypeError: _firebaseService2.default.onlineUsers(...).then is not a function
  </code></pre>
  <p>in addition,
  when I do this way</p>
  <pre><code>   this.unsubscribe = firebaseService.onlineUsers().then((querySnapshot)=&gt;{
          console.log(querySnapshot.size)
          this.setState({count:querySnapshot.size})
      })
  </code></pre>
  <p>// other file</p>
  <pre><code> onlineUsers(callback) {
      return this.status_database_ref.where('state', '==', 'online').get()
  }
  </code></pre>
  <p>it not listen to change into firebase, means if I change in firebase it's not update or change the size..</p>
  <p>---- firestore function ---
  I tried to make firestore function that trigger each time the UserStatus node updated but this take some seconds and it slow for me.</p>
  <pre><code>module.exports.onUserStatusChanged = functions.database
  .ref('/UserStatus/{uid}').onUpdate((change, context) =&gt; {
      // Get the data written to Realtime Database
      const eventStatus = change.after.val();
  
      // Then use other event data to create a reference to the
      // corresponding Firestore document.
      const userStatusFirestoreRef = firestore.doc(`UserStatus/${context.params.uid}`);
  
  
      // It is likely that the Realtime Database change that triggered
      // this event has already been overwritten by a fast change in
      // online / offline status, so we'll re-read the current data
      // and compare the timestamps.
      return change.after.ref.once(&quot;value&quot;).then((statusSnapshot) =&gt; {
          return statusSnapshot.val();
      }).then((status) =&gt; {
          console.log(status, eventStatus);
          // If the current timestamp for this data is newer than
          // the data that triggered this event, we exit this function.
          if (status.last_changed &gt; eventStatus.last_changed) return status;
  
          // Otherwise, we convert the last_changed field to a Date
          eventStatus.last_changed = new Date(eventStatus.last_changed);
  
          // ... and write it to Firestore.
          //return userStatusFirestoreRef.set(eventStatus);
          return userStatusFirestoreRef.update(eventStatus);
      });
  });
  </code></pre>
  <p>function to calculate and update count of online users</p>
  <pre><code>module.exports.countOnlineUsers = functions.firestore.document('/UserStatus/{uid}').onWrite((change, context) =&gt; {
  
      const userOnlineCounterRef = firestore.doc('Counters/onlineUsersCounter');
  
      const docRef = firestore.collection('UserStatus').where('state', '==', 'online').get().then(e =&gt; {
          let count = e.size;
          return userOnlineCounterRef.update({ count })
      })
  })
  </code></pre>