Christian Stewart
Christian Stewart

Reputation: 15519

Meteor Session Replacement?

In the latest Meteor release (version 0.5.8), Session has been removed from the server-side code.

Previously I've used Session to store client-specific variables for the server; what is the replacement for this functionality?

Example case: User One opens a browser, User Two opens a browser. One calls a method on the server setting some token, the other calls a method on the server doing the same. I then need to access this when the client requests something. How do I differentiate between the two?

Upvotes: 9

Views: 3317

Answers (2)

Andrew Wilcox
Andrew Wilcox

Reputation: 1021

You'll want to save your tokens to a collection in the database.

You could use a Session on the server if you wanted to simply by copying the session package into your application's packages directory and changing its package.js to also load on the server. But a Session is an in-memory data structure, and so won't work if you have multiple server instances; and you wouldn't be able to restart the server without losing your user's tokens.

If you store your tokens in the database they'll persist across server restarts, and will work with a future version of Meteor which is able to scale an application by adding more server instances when needed.

If you need to expire your tokens (so that your collection doesn't grow without bound), you could add a "lastUsed" Date field to your token collection, and periodically remove tokens that haven't been used for longer than your chosen expiration period.

Upvotes: 7

Tarang
Tarang

Reputation: 75945

You can use each one's session id which is unique to the tab too. Not too sure how to get the current session id but it should be there somewhere (you can see it in Meteor.default_server.sessions, so there is still a way:

Client js

Meteor.call("test", Meteor.default_connection._lastSessionId, function(err,result) {
    console.log(result);
});

Server side Js

Session = {
  set : function(key, value, sessionid) { 
      console.log(Meteor.default_server.sessions[sessionid]);
      if(!Meteor.default_server.sessions[sessionid].session_hash) Meteor.default_server.sessions[sessionid].session_hash = {};
      Meteor.default_server.sessions[sessionid].session_hash.key = value;
  },
  get : function(key, sessionid) {
      if(Meteor.default_server.sessions[sessionid].session_hash)
        return Meteor.default_server.sessions[sessionid].session_hash.key;
  },
  equals: function(key, value, sessionid) {
      return (this.get(key, sessionid) == value)
  },
  listAllSessionids: function() {
      return _.pluck(Meteor.default_server.sessions, "id");
  }  
};


Meteor.methods({
    test:function(sessionid) {

        if(!Session.get("initial_load", sessionid)) Session.set("initial_load", new Date().getTime(), sessionid);

        return Session.get("initial_load", sessionid);
    }
});

I hook into Meteor.default_connection._sessions to store the values so that theres some type of garbage collection involved when the session isn't valid anymore (i.e the user has closed his tabs) to prevent memory being wasted. In livedata_server.js these old sessions get destroyed after 1 minute of no activity on the DDP wire (like the heartbeat).

Because the server can see everyone's session you can use the sessionid to access another user's session data. and listAllSessionids to give out an array of all the sessionids currently active.

Automatically set session like this.userId in a Method without using a param in a call

It looks like there is functionality for this this but its not fully hooked up. The session id would be stored in this.sessionData but its likely still unfinished. Its there to be called in method but theres nowhere that its being set yet (in livedata_connection.js & livedata_server.js)

Upvotes: 2

Related Questions