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?
Session provides a global object on the client that you can use to store an arbitrary set of key-value pairs. Use it to store things like the currently selected item in a list.
Sessions are used for saving data while the users are using the app. This data will be deleted when the user leaves the app. In this chapter, we will learn how to set a session object, store some data, and return that data. We will use the basic HTML setup.
What is Meteor? Meteor is a full-stack JavaScript platform for developing modern web and mobile applications. Meteor includes a key set of technologies for building connected-client reactive applications, a build tool, and a curated set of packages from the Node. js and general JavaScript community.
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.
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