Curtis Larson's Blog    Main Site    Portfolio     Archive     Feed

Meteor - How to Trigger a Function when a Session Value Changes

How to Trigger a Function when a Session Value Changes

While working on a Meteor project, I wanted to trigger a function whenever I changed the value of a Session variable through Session.set(). It took me a little bit of experimentation and digging to figure out how to do it, and the result is pretty simple.

How to do it

Use the following code:

var doSomething = function() {
  // Do something when the session value changes

Tracker.autorun(function() {
  var sessionVal = Session.get("yourSessionVariable");
  console.log("The session value has changed");

var anotherFunction = function() {
  Session.set("yourSessionVariable", "foo");

That’s it! Whenever anotherFunction is executed, or whenever the session value yourSessionVariable is changed in any other function, the function in the Tracker.autorun() block will execute. Simple!


Tracker.autoRun() is the key here, as it can make arbitrary blocks of code reactive. Session is already a reactive variable, so we do not need to go through the trouble of using Tracker.Dependency. You can find more information about Tracker here.

Curtis Larson is a LA based freelance software developer. Follow him on Twitter or send him an email if you're looking for a talented developer.