percolatestudio / publish-counts

Meteor package to help you publish the count of a cursor, in real time
https://atmospherejs.com/tmeasday/publish-counts
MIT License
200 stars 46 forks source link

Count not updating when find() selector changes #82

Closed abecks closed 8 years ago

abecks commented 8 years ago

I have a publication that looks like this:

    Counts.publish(this, 'CompanyInfiniteCount', app.collections.Company.find(query), {noReady: true});

    return app.collections.Company.find(query, {
      limit: limit,
      sort: sort
    });

Designed to give me the total count of company records, and then returning a limited cursor for pagination.

When query changes (to filter the list), the result of Counts.get('CompanyInfiniteCount') is not changing from it's initial value.

I am trying to achieve the same thing as this example in the docs:

Meteor.publish('posts-with-count', function() {
  Counts.publish(this, 'posts', Posts.find(), { noReady: true });
  return Posts.find({}, { limit: 10 });
});

But with a changing document selector. Am I using Counts wrong?

DominikGuzei commented 8 years ago

Same for me, any solution?

boxofrox commented 8 years ago

@abecks.

I assume your Meteor.publish callback takes a query argument unlike the given example from the docs. If so, then you might be running into multiple Counts.publish conflicting because they all have the same name, 'CompanyInfiniteCount'.

I don't recall if Meteor automatically stops an existing subscription if you subscribe again with different arguments. If not, then save the handle returned by Meteor.subscribe and call handle.stop() before running Meteor.subscribe with the new query/selector.

There may be some overlap with an issue described in #66 in this comment.

If you can generate a very basic Meteor app that demonstrates this problem and upload it to github with a link in a comment here, I'll fidget with it and come up with some better advice.

abecks commented 8 years ago

@boxofrox you are 100% correct. I actually ended up abandoning Counts and I'm now periodically polling a server method for the count as needed. Still a great package!

boxofrox commented 8 years ago

@abecks glad you found a solution.

@DominikGuzei if you're still seeking a solution for this issue, let's begin in a new issue.

DominikGuzei commented 8 years ago

Haha, found the same "solution" as @abecks – polling methods, since that's the only performant way to count large collections. The realtime aspect is much less important for us :wink:

But btw. my solution to the problem discussed here was to separate the counter publication from another one. So the problem seemed to be related with using Counts.publish and then returning another record set like Meteor.users.find(…) in the publication