Skip to content

Instantly share code, notes, and snippets.

@zeroasterisk
Forked from matteoagosti/client.js
Last active December 16, 2015 16:40
Show Gist options
  • Save zeroasterisk/5465178 to your computer and use it in GitHub Desktop.
Save zeroasterisk/5465178 to your computer and use it in GitHub Desktop.
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
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('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;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment