Announcing Persistent Session for Meteor


Meteor’s Session object forgets its values when the user closes or even refreshes their browser window, and isn’t shared between browser tabs. If you wish to use the Session object to store data that’s shared between browser tabs, or long term data, such as user preferences or recent items, you should use our persistent-session module.

Persistent-session is a drop-in replacement for Meteor’s Session that will persist Session variables across browser refreshes. After installing it, all of your session variables will be persisted, with no further action required on your behalf. The original session behaviour is still accessible, should you require it (such as for loading indicators or spinners).

How to use

Installation is done using meteorite:

mrt add persistent-session

That’s it!

Use Session.set() and Session.get() normally. The values will be persisted across browser sessions.

If you want set() to behave as it does normally in Meteor, use Session.setTemporary(), which will cause the key-value-pair not to persist, and it will be lost upon browser refresh.

If you want to clear all set values, you can call Session.clear(), this will clear all key-value-pairs set using Session.set() or Session.setTemporary().

By default, Session.clear() is called when the user signs-out. To disable this behaviour, set persistent_session.clear_on_signout to false in your config/settings.json file:

{
  "public": {
    "persistent_session": {
      "clear_on_signout": true
    }
  }
}

How does it work?

Persistent Session takes the original Meteor Session and wraps it with new functions that also use the browser’s localStorage as a data store. Session.setTemporary() directly calls the original Session.set.

It uses amplifyjs’s store library to save values to localStorage, falling back to other solutions if it’s not available.

Let's stay connected. Join our monthly newsletter to receive updates on events, training, and helpful articles from our team.