Carpet4
Carpet4

Reputation: 610

meteor autorun responds slower than helper

i've been battling for a few days with an autorun function that renders elements on a canvas for two players in a board game. the whole process from when 1 player places a move until its visible for the other player took too long. i managed to cut alot by optimizing the code i have written as a meteor beginner, and also by placing the method that updates the game move inside a (meteor.isServer) so it won't first run the method in the move maker's client (it was already just inside the server's folder). one delay in time which is beyond my understanding, happens from the moment the server finishes with the move making method until the client's autorun begin to run(about 60 ms delay for chrome, and 150(!) for firefox). i've ran some tests to try and narrow down the options for why it happens, the last one was to subscribe to the games collection from a different page, and see if the response time is slow there too. this is the code:

Template.Watch.onCreated(function(){
  this.subscribe('activeGames');
  this.autorun(()=>{
    if(this.subscriptionsReady()){
        console.log(Games.findOne().turn + " " + (new Date).getTime());
    }
  });
});


Template.Watch.helpers({

  games: ()=> {
    console.log(Games.findOne().turn + " " + (new Date).getTime());
    return Games.find({result: false});
  }

});

now, on the client both console.logs appeared whenever a move was played, the weird thing is that the helper's console.log was fired with no delay, 50 ms before the autorun's console.log which had the same delay as in the game page. by my poor understanding, if the client knows a change was made to the database, the autorun should simply run, just like the helper simply runs. i would be very grateful to whoever explains to me the behavior of the autorun that causes that delay, and if there is a way to cancel it.

Thanks in advance!

Upvotes: 0

Views: 171

Answers (1)

Pankaj Jatav
Pankaj Jatav

Reputation: 2184

When you call a method on the client using Meteor.call, two things happen in parallel:

  1. The client sends a request to the server to run the method in a secure environment, just like an AJAX request would work A simulation of the

  2. Method runs directly on the client to attempt to predict the outcome of the server call using the available information.

So here is the reason why your Helper console print before the autorun.

  • Your mini-mongo(client side DB) get updated before the server data update according to point number 2.
  • According to point number 1, the client sends a request to the server, then server updates the server database. Server re-publish the data to the client and autorun reinitialize in your case.

So the point is your mini mongo(client side DB) updated before the subscription ready. that's why you got console log in helper before your autorun.

One more thing happen when your server data to client::

If the result from the server comes back and is consistent with the simulation on the client, everything remains as is. If the result on the server is different from the result of the simulation on the client, the UI is patched to reflect the actual state of the server.

Upvotes: 1

Related Questions