ReactiveMethod to update template periodically

template.js

import { ReactiveMethod} from 'meteor/simple:reactive-method';

Template.staging.helpers({
	'status': function () {
		let status = ReactiveMethod.call('Staging.status');

		return status;
	}
});

template.html

<template name="staging">
	<h1>Staging</h1>

	<table class="table">
		<tr><th>New Documents</th><td>{{status.new}}</td></tr>
		<tr><th>Documents in Error</th><td>{{status.error}}</td></tr>
	</table>
</template>

I have tried multiple different versions of this, which always lead to ReactiveMethod complaining it can’t be used outside of a Tracker Computation.

Ideally I’d want the values to update for example on a set interval of 5 seconds, but I can’t wrap ReactiveMethod.call to interval without “…outside Tracker Computation”-error.

How to achieve this without having to publish anything from the collection to the client - I just want counts via ReactiveMethod.call?

Now only way to get updated values is by refreshing browser, which is far from ideal…

If you want to control the interval manually, why use ReactiveMethod?
The point of that one is to be… reactive?
Just use a normal method call

1 Like
Template.staging.onCreated(function(){
   var instance = this;
   instance.status = new ReactiveVar();
   instance.statusCheck = function(){
      Meteor.call("checkStatus", function(e,r){
          instance.status.set(r)
      }
   }
   setInterval(instance.statusChec, 5000);
})

Template.status.helpers({
   status: function(){
       return.Template.instance().status.get()
   }
});
2 Likes