Guig
Guig

Reputation: 10429

Meteor - call method from client & server method

What will happen if from a method that is shared by the client and the server I call another method that is on the server only? Will it get called twice? Only once from the server? Only once from the client?

//lib/methods.js
Meteor.methods({
  test: function() {
    /*do some stuff that needs to update the UI quickly*/
    Meteor.call('doSomeSecureStuff', Meteor.isClient);
  }
});

//server/methods.js
import secureStuff from './secureStuff.js';
Meteor.methods({
  doSomeSecureStuff: function(originIsClient) {
    console.log(originIsClient);
    secureStuff();
  }
});

From my tests it only gets called once from the server, but since I've found no doc on that I wanted to make sure 1) this is what actually happen and 2) will stay like this in the future

(As suggested by the example, a use case for which I can't just wrap the server part in Meteor.isServer is when I need to load code that is only available on the server)

Upvotes: 2

Views: 1316

Answers (1)

sys13
sys13

Reputation: 148

Yes, only once on the server.

You can wrap the server part of a shared method with this.isSimulation

When you run a shared method it first runs a simulation on the client and then on server - updating the client with its results (which are usually the same - which is why it's called Optimistic UI).

Upvotes: 3

Related Questions