Meteor - Session

meteor - done reading

What is a session?

Sessions allow us to store small pieces of data that:

  1. Are not saved to the database.
  2. Will not be remembered on return visits.

This sort of data might not sound immediately useful, but as we’ll see, it’s a surprisingly versatile way of solving a lot of common problems when building a web application (and, specifically, when building an interface). To begin using sessions, we need to first install a package:

meteor add session

Prior to Meteor 1.3, the functionality for creating sessions was included with every project, but it has since been removed from the core framework and turned into a package. Installing packages, fortunately, is quite simple.

Sessions are an abstract concept, so the best way to understand them is to create one, allowing us to learn by example. o create a session, add the following code inside the “click .player” event:

Session.set('selectedPlayer', 'session value test');

Here, we’re using this Session.set function and passing through two values:

  1. First, we’re passing through a name for the session. This name is simply a reference for the session. It’s what allows us to refer to the session at a later time. In this case, we’re defining the name as “selectedPlayer”, but you’re free to use whatever name you like.
  2. Second, we’re passing through a value for the session. This is the data we’re storing inside the session. In this case, we’re passing through a string of “session value test”.

To retrieve the value of this session, we can use a Session.get function:

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License