Hello all!
I am having a little problem that i cannot manage to sort out…
i’m doing huge processing server side and i would like to keep track of the state and show it on the client side. For that purpose i have a variable that i’m updating as the process go through. To keep track of it i’m using that client side:
Template.importJson.onCreated(function () {
Session.set('import_datas', null);
this.autorun(function(){
Meteor.call('readImportState', function(err, response) {
console.log(response);
if (response !== undefined) {
Session.set('importingMessage',response);
}
});
})
});
I’m reading it from template that way (in template.mytemplate.helpers):
readImportState: function() {
return Session.get('importingMessage');
},
And here is the server side code to be called by meteor.call:
readImportState: function() {
console.log(IMPORT_STATE);
return IMPORT_STATE;
}
The client grab the value at start but it is never updated later… What am i missing here? If somebody could point me in the right direction that would be awesome. Thank you 
There is no reactive change to cause your autorun
to re-run. Once set, your Session
variable will not be changed outside of the autorun
and there is no other reactive dependency.
There are a number of ways you can make this work. I’ve indicate the simplest refactor here:
Change your client-side autorun
to Meteor.setInterval
to use polling:
Template.importJson.onCreated(function () {
Session.set('import_datas', null);
Meteor.setInterval(() => {
Meteor.call('readImportState', function(err, response) {
console.log(response);
if (response !== undefined) {
Session.set('importingMessage',response);
}
});
}, 1000);
});
In practice you should cancel the setInterval
when you receive the “finished” message or you will continue polling needlessly.
Disadvantages: It’s not actually reactive and the “best” interval to use may need research.
There are truly reactive solutions which drive the client directly from the server, some of which can end up quite complex. A simple solution is to use a dedicated collection for tracking status, which gives reactive changes from server to client with almost no work.
1 Like
Thank you for your reply,
i think i will use the publishing solution, because it is of little interest to use an interval. I was thinking i could use some auto updated reactive vars somehow.
Thanks 