Do Meteor sessions get reset when there is a page refresh?
For some reason I did not think they did but it seems like they do. Is there a way to make them persist?
If not what would be the best solution for this?
I want to allow the same data to show if a user refreshes (this data is specific to the user) even if they are not registered yet.
Actually what you could do is create a "subclass" of Session that stores the value in Amplify's store when set() is called. You would automatically inherit all the reactive properties of Session. Here is the code, it worked for me:
SessionAmplify = _.extend({}, Session, { keys: _.object(_.map(amplify.store(), function(value, key) { return [key, JSON.stringify(value)] })), set: function (key, value) { Session.set.apply(this, arguments); amplify.store(key, value); }, });
Just replace all your Session.set/get calls with SessionAmplify.set/get calls. When set() is called, the parent Session method is called, as well as amplify.store(). When the "subclass" is first created, it loads everything that is in amplify's store inside its keys, so that they can be retrieved right away with get().
You can test a working variation of the Leaderboard example here: https://github.com/sebastienbarre/meteor-leaderboard
This is an old question, but it's the second hit on a search for "meteor session manager", so I think it's important to add that the package u2622:persistent-session solves this issue perfectly.
from the docs at: https://atmospherejs.com/u2622/persistent-session
Installation
meteor add u2622:persistent-session
That's it! Now you can use Session.setPersistent to set a session variable that will save after a refresh.
If you'd like, you can have Session.set do this as well. See the Options section below.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With