Documentation Index
Fetch the complete documentation index at: https://mintlify.com/meteor/meteor/llms.txt
Use this file to discover all available pages before exploring further.
Session API
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. Source:packages/session/session.js
Note: Session is a ReactiveDict instance. All methods are reactive data sources.
Locus: Client only
Session.set()
Set a variable in the session. Notify any listeners that the value has changed (eg: redraw templates, and rerun anyTracker.autorun computations that called Session.get on this key).
The key to set, eg,
selectedItemThe new value for
key. Can be any EJSON-compatible value, or undefined.Session.setDefault()
Set a variable in the session if it hasn’t been set before. Otherwise works exactly the same asSession.set.
The key to set, eg,
selectedItemThe new value for
keySession.get()
Get the value of a session variable. If inside a reactive computation, invalidate the computation the next time the value of the variable is changed bySession.set. Returns a clone of the session value, so if it’s an object or an array, mutating the returned value has no effect on the value stored in the session.
The name of the session variable to return
undefined if not set.
Session.equals()
Test if a session variable is equal to a value. If inside a reactive computation, invalidate the computation the next time the variable changes to or from the value.The name of the session variable to test
The value to test against
boolean - True if values are equal.
Complete Example
Persistence
By default, Session variables are not persisted across page reloads. If you need persistence, consider using:localStoragefor simple key-value storageReactiveDictwith a name parameter for named reactive dictionaries- Third-party packages like
u2622:persistent-session
Best Practices
- Use for UI state only: Session is best for temporary UI state like selected items, current page, or modal visibility.
-
Initialize with setDefault: Use
Session.setDefault()to set initial values to avoid undefined errors. -
Use equals() for comparisons:
Session.equals()is more efficient thanSession.get()when checking for specific values. - Clear on navigation: Consider clearing session variables when navigating between routes to avoid stale state.