Reputation: 21
first question here but i really don't know where to go. I cannot find anything that help me on google.
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 :)
Upvotes: 2
Views: 99
Reputation: 64312
As of this writing, the only easy way to share reactive state between the server and the client is to use the publish/subscribe mechanism. Other solutions will be like fighting an uphill battle.
Here's the (incorrect) solution you are looking for:
jobStates
, where jobStates
is an object with user ids as its keys, and state strings as its values.autorun
doesn't work for Meteor.call
(there is no reactive state forcing the autorun
to execute again) - you'd need to actually poll every N seconds via setInterval
.jobStates
.setInterval
.jobStates
.jobStates
. Alternatively, the method from (2) should just read the database instead of actually keeping jobStates
in memory.This approach is complicated and error prone. Furthermore, it requires writing the state to the database anyway in order to handle restarts and multiple server instances.
find
or findOne
in a template to display the state to the user.As you can see, the publish/subscribe mechanism is considerably easier to implement because most of the work is done for you by meteor.
Upvotes: 1