1

Is there a way to let code on the server react to a Session.variable on the client?

eg:

if(Meteor.isClient() {
    Template.main.events({
        'click #appleBtn': function() {
            Session.set('fruit', 'apples')
        }
    })
}


if(Meteor.isServer) {
    if(Session.get('fruit') == 'apples') {
        doSomething()
    } else {
        doAnotherThing()
    }   
}

My initial idea is to have a clientside code continuously send the value of the session variable to the server via a method call, but that doesnt seem too efficient.

1
  • The code you have posted crashes because there is no Session on the server, it's not very interesting. Can you instead post the code which uses a method call to send the Session data and explain why it is does not "seem too efficient"? Commented Oct 23, 2015 at 13:40

2 Answers 2

2

Sessions don't work on server-side, but your initial idea is a good start.

Instead of continuously sending that session value just have a template helper on the client that gets the session value and calls a Meteor method with that value. This way only when an update happens to the session variable will that client helper react to the change and call the Meteor method with updated values.

// Client
Template.main.helpers({
    reactiveHelper: {
        var reactiveValue = Session.get('fruit');
        Meteor.call('someMethod', reactiveValue);
    }
});

// Templates where you want this to happen
{{reactiveHelper}}

// Server
Meteor.methods({
    'someMethod': function(reactiveValue) {
        // Server code that reacts to client session changes
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Have you tried Tracker.autorun?

Tracker.autorun(function () {
    Meteor.call('someMethod', Session.get('fruit'), function (err, res) {
        // do something with the result...
    });
});

The method will only be called when the Session var changes (after running once with the initial value of Session.get('fruit'))

On the server you'd do:

Meteor.methods({
    someMethod: function (fruit) {
        if (fruit === 'apple') {
            doSomething();
        } else {
            doSomethingElse();
        }
    }
});

EDIT: RE my comment below, an example of doing this entirely inside a single template:

Template.MyTemplate.onCreated(function () { 
    this.fruit = new ReactiveVar('orange'); 
    var instance = this; 

    instance.autorun(function() {
        Meteor.call('myMethod', instance.fruit.get(), function (err, res) {
            // do something?
        });
    }); 
});

Template.MyTemplate.events({
    'click #myButton': function (event, tmpl) {
        tmpl.fruit.set('apple');
    }
});

4 Comments

This is how I would handle it.
Just to let you know: The answer from dard12 does the same. In case of that "Session.get" inside the helper method, this would be also only called, when value has changed. You could say that an implicit Tracker.autorun is around in that helper. This is happen due to Session vars.
@TomFreudenberg Well yes, but there was no indication from the OP that the Session value could only be changed in one template, therefore a 'global' Tracker.autorun may be required. If it's only used in a single template then a global Session variable isn't required in the first place... And although the helper method works it's not strictly the 'correct' way to do things inside a template - I'll add an example of that to my answer.
@MichaelMason my comment is just a "do you know that" comment. But as Session is global it will automatically reflect to all such parts in the app regardless how often this value is used or changed. But of course, your edited approach for template level maintained value is absolutely correct.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.