Publish/subscribe per time interval in Meteor

319 Views Asked by At

My situation is as follows: I have a collection in Mongo which gets updated with new items every few milliseconds, for example log items. I'm displaying these items on the frontend via publish/subscribe in a template, but because of the high volume the list updates so frequently that it's hard to read them. What I would like is to only have the list be updated every (few) seconds. I have tried to use sleep/timeouts on both the client and server side, as indicated here for example, without success so far.

  • Can I still use publish/subscribe for this or should I switch a polling mechanism with Meteor.setInterval?
  • Should the time interval part be on the publish or on the subscribe side?
  • If publish/subscribe is correct for my scenario, how do I only show the updated data every few seconds?
2

There are 2 best solutions below

0
On

DDP has a rate limiter. it's meant for defeating DDoS attacks, but i suppose it could be repurposed for what you want.

https://blog.meteor.com/rate-limiting-in-meteor-core-762b8ad2412f#.nw6fwlhji

4
On

You should be able to use reactive variables and autorun in your Template.name.onCreated to do this :

Template.name.onCreated(function(){
var instance = this;
instance.now = new ReactiveVar( new Date());
instance.autorun(function(){
   var test = now.get();
   instance.subscribe('yourSubNameHere');
   setTimeout(function(){ //will update now and fire the autorun again
      instance.now.set(new Date());
   },timeoutHere)
});
)};

Although if your collection gets big I'd advise doing this with a limit in your publication maybe?