Search code examples
javascriptmeteorlogicpublish-subscribe

Publish only things who were read 10seconds ago from now


I need to publish the stats from my CPU, Memory and other things that are stored in a collection. The things is it need to publish only the 10 last seconds from now.
I join a picture to help the understanding of this:enter image description here

And I coded a function but the problem is that it doesn't remove the infos that are 11sec,12sec... older than now.

Meteor.publish('dockerStats', function readInfosDockerStats(timeLimitSecond) {
        console.log(moment(new Date()).subtract(timeLimitSecond, 'second').toDate())
        return DockerStats.find(
               { read: { "$gte": moment(new Date()).subtract(timeLimitSecond, 'second').unix()} },
);

I think I need to use $and and to find infos that are

>now-10sec && <now

But I don't know how to make it so I'm asking for your help.

[EDIT] I added the $and the only problem is that it publish only 1time (the infos keep being writing in the collection but it doesn't publish them):

Meteor.publish('dockerStats', function readInfosDockerStats(timeLimitSecond) {
  return  DockerStats.find(
      { $and: [{ read:{"$gte": moment(new Date()).subtract(timeLimitSecond, 'second').unix()}},{read:{"$lte":  moment(new Date()).unix()}}]
    })
});

Solution

  • Meteor publications work in a way that is causing you this problem...

    Data is published selectively from the server. But once published, it is not removed from the client side collection. So your client side data is growing over time as more and more data is published.

    So you will need to do a filter in your client code (ie your subscription) as well. That will fix the problem. It will, however, rely on exact time synchronisation between client and server.

    Over time your client side collection will also grow, and eventually cause you some memory/performance problems. So you might want to consider a different approach. You could create a separate collection which is managed by the server, to just contain the last 10s of data. Older data is deleted, and the client subscription is simple, with no need for precise clock sync between client and server.