title: Meteor: using Sessions and ReactiveVar
body: <p>When should you use <code>Session</code> and <code>ReactiveVar</code>? I use <code>Session</code> variable as a communication medium between components. Let's look at Stackoverflow for example.</p>
  
  <p><a href="https://i.stack.imgur.com/aUR0h.png" rel="noreferrer"><img src="https://i.stack.imgur.com/aUR0h.png" alt="enter image description here"></a></p>
  
  <p>I labeled three hypothetical components. Let's look at the <code>filters</code> component. If you click <code>Tags</code> then the <code>Main</code> components will show its questions based on your favorite tags. To do this, I will set <code>Session.set("Main/showTags", true)</code> when the <code>Tags</code> button is clicked. In the Main component, I will have a helper function like below:</p>
  
  <pre><code>Template.Main.helpers({
    posts: function() {
      var isTags = Session.get("Main/showTags");
      var isQuestions = Session.get("Main/showQuestions");
      ...
      if (isTags) {
        return Posts.find().sort({tags: 1}) // or something along the lines
      } else if (isQuestions) ...
    }
  });
  </code></pre>
  
  <p>This has worked well in most cases but I've seen from a lot of places that I should refrain from using <code>Session</code> and use <code>ReactiveVar</code>. But If I use <code>ReactiveVar</code> everywhere, then I would need the reference to all template instances. I expect it to work well between direct parent and children templates (ex. inside the main component, there could be <code>VoteAnswerViewsButtonTemplate</code>) but how would you do it with <code>ReactiveVar</code> when you want independent components to talk to each other?</p>
  
  <p>Here's my final question. How can I appropriately use <code>Session</code> and <code>ReactiveVar</code> to keep the scope of components and make them communicate with each other as well? Also, if I use <code>Session</code> the way I do now, am I polluting the global namespace unnecessarily?</p>
  
  <p>Related Documents:</p>
  
  <ol>
  <li><a href="https://dweldon.silvrback.com/scoped-reactivity" rel="noreferrer">https://dweldon.silvrback.com/scoped-reactivity</a></li>
  <li><a href="https://www.discovermeteor.com/blog/reactivity-basics-meteors-magic-demystified/" rel="noreferrer">https://www.discovermeteor.com/blog/reactivity-basics-meteors-magic-demystified/</a></li>
  </ol>