Meteor Sessions

Last Updated : 31 Dec, 2021

Meteor is a full-stack JavaScript platform that is used for developing modern web and mobile applications. Meteor has a set of features that are helpful in creating a responsive and reactive web or mobile application using JavaScript or different packages available in the framework. It is used to build connected-client reactive applications.

On the client, Session provides a global object that may be used to hold an arbitrary set of key-value pairs. It can be used to save information such as the currently chosen item in a list. When the user exits the app, the data will be removed.

Syntax:

Session.set(key, value);

Run the following command in your terminal to add Session to your application:

meteor add session

Creating Meteor Application And Importing Module:

Step 1: Create a React application using the following command.

meteor create foldername

Step 2: After creating your project folder i.e. foldername, move to it using the following command.

cd foldername

Step 3: Import Session module from 'meteor/session'

import { Session } from 'meteor/session'

Project Structure: It will look like the following.

Step to Run Application: Run the application from the root directory of the project, using the following command.

meteor

Example: This is the basic example that shows how to use the Sessions component.

Main.html
<head>
    <title>GeeksforGeeks</title>
</head>

<body>
    <div>
        {{> employee}}
    </div>
</body>

<template name="employee">
    <h1 class="heading">GeeksforGeeks</h1>
    <p>
        Session provides a global object that may
        be used to hold an arbitrary set of key-value pairs.
    </p>

</template>
Main.js
import { Session } from 'meteor/session';
import './main.html';

var emp = {
    FirstName: "John",
    LastName: "Smith",
    Age: 10,
    Designation: "System Architecture"
}

Session.set('data', emp);

var display = Session.get('data');
console.log(display);
Comment