2

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)

Guig
  • 9,891
  • 7
  • 64
  • 126
  • Methods are only on the server side. they don't run on the client. the client calls the server method – MrE Mar 10 '16 at 01:11
  • 1
    This is not true: the methods run on the client as well as a simulation to give faster UI update, and when the response from the server comes, the results are compared, and updated according to the server if needed. http://docs.meteor.com/#/full/meteor_methods – Guig Mar 10 '16 at 01:37
  • It's only true when you define your methods explicitly for the server, either in `/server/` or within `if (Meteor.isServer) { ... }` – Guig Mar 10 '16 at 01:38

1 Answers1

3

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).

sys13
  • 148
  • 9
  • Yeah, I was wondering how bounded the simulation is (can it change UI that the server response won't be able to fix? Here: can it call other methods? can it make call to external scripts, like logging metrics etc?) – Guig Mar 10 '16 at 01:40
  • The server response only updates the state of minimongo (the mongo cache in the client). Not sure about the calls to external scripts. – sys13 Mar 10 '16 at 01:42
  • I've just tested: if you do `if (Meteor.isClient) {$.get('https://test.com/');}` within your method, the request is made, so it's unclear why `Meteor.call` is not run from the client (even if it make sense because the method being ran on both sides, you might not except its effect to multiply) – Guig Mar 10 '16 at 01:48