title: Firebase Realtime Listener Concept - how to detach / unsubscribe from the listener?
body: <p><a href="https://firebase.google.com/docs/firestore/query-data/listen" rel="nofollow noreferrer">Get realtime updates with Cloud Firestore</a> shows how to subscribe to, or listen to, document changes.</p>
  
  <pre><code>db.collection("cities").doc("SF")
      .onSnapshot(function(doc) {
          console.log("Current data: ", doc.data());
      });
  </code></pre>
  
  <p>The example above makes sense. My question concerns detaching the listener. Is it accurate to say that when we want to detach / unsubscribe / unlisten to a document, we simply subscribe a second time to the same database reference? </p>
  
  <p>...that's what it looks like, but I'm not crystal clear as to how/why?</p>
  
  <pre><code>var unsubscribe = db.collection("cities")
      .onSnapshot(function (){
        // Respond to data
        // ...
      });
  
  // Later ...
  
  // Stop listening to changes
  unsubscribe();
  </code></pre>
  
  <p>This seems unnecessarily confusing. ...Why not simply have 2 methods, <code>.onSnapshot</code> and <code>.offSnapshot</code>? I supposed the only difference I see between the two provided examples is that the latter is assigned to a variable and that the method is called on the whole collection, rather than a single document.</p>
  
  <p>So, is the bottom line: to unsubscribe, attach the <code>.onSnapshot</code> method to the parent collection and assign it to a variable?</p>