Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meteor Session and browser refreshes

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.

like image 609
Jonovono Avatar asked Nov 14 '12 00:11

Jonovono


2 Answers

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

like image 126
sebastien.b Avatar answered Oct 19 '22 12:10

sebastien.b


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.

like image 40
Wes Avatar answered Oct 19 '22 11:10

Wes