Skip to content

Instantly share code, notes, and snippets.

@matteoagosti
Created June 3, 2012 21:49
Show Gist options
  • Star 7 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save matteoagosti/2865146 to your computer and use it in GitHub Desktop.
Save matteoagosti/2865146 to your computer and use it in GitHub Desktop.
Meteor JS server side sessions
Meteor.subscribe(
'server_sessions',
amplify.store('session'), // Read from local storage / cookies
function() {
// The server returns only one record, so findOne will return that record
var serverSession = new Meteor.Collection('server_sessions').findOne();
// Stores into client session all data contained in server session;
// supports reactivity when server changes the serverSession
Session.set('serverSession', serverSession);
// Stores the server session id into local storage / cookies
amplify.store('myapp.session', serverSession._id);
}
);
// Do not have this in a model.js file, its scope should be server only
ServerSessions = new Meteor.Collection('server_sessions');
Meteor.publish('server_sessions', function(id) {
var created = new Date().getTime();
// If no id is passed we create a new session
if(!id) {
id = ServerSessions.insert({created: created});
}
// Load the session
var serverSession = ServerSessions.find(id);
// If no session is loaded, creates a new one;
// id no longer valid
if(serverSession.count() === 0) {
id = ServerSessions.insert({created: created});
serverSession = ServerSessions.find(id);
}
return serverSession;
});
@inspire22
Copy link

How would you go about removing old sessions?

@dandv
Copy link

dandv commented Dec 9, 2013

The user-session package seems to do just that - server-side persistence across devices and sessions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment