Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

just wondering how well does this package play along with publish-composite #43

Open
lsunsi opened this issue Jun 25, 2015 · 5 comments
Labels

Comments

@lsunsi
Copy link

lsunsi commented Jun 25, 2015

I tried publishing some counts in a nested publication function and I'm getting some odd behavior. Anyway, it's just a general question

@boxofrox
Copy link
Contributor

boxofrox commented Aug 4, 2015

As I understand it...

I'm not surprised by the odd behavior. publish-counts docs don't speak much about the hazards of having multiple counts. Primarily, you must ensure every subscription uses a unique name for the count, otherwise subscriptions will overwrite the same count, leading to errors. Or another way to put it, never subscribe to the same count twice (there are exceptions).

It should be possible to use publish-counts and publish-composite together. I expect it will be very redundant.

This example is rushed and untested, so there may be some errors.

if (Meteor.isServer) {
  Meteor.publishComposite('topTenPosts', {
    find: function () {
      var cursor = Posts.find({}, { sort: { score: -1 }, limit: 10 });
      // counter will always be ten *shrug*
      Counts.publish(this, 'topTenPostsCount', cursor, { noReady: true });
      return cursor;
    },
    children: [
      {
        find: function (post) {
          // find post author
          return Meteor.users.find(
            { _id: post.authorId },
            { limit: 1, fields: { profile: 1 } });
        }
      },
      {
        find: function (post) {
          // find comments on post
          var cursor = Comments.find(
            { postId: post._id },
            { sort: { score: -1 } });
          Counts.publish(
              this,
              'commentCountForPost' + post._id, // the id is necessary to ensure
                                                // publish-counts saves the number
                                                // of comments for each post as a
                                                // separate document in the Counts
                                                // Collection.
              Comments.find({postId: post._id}),
              { noReady: true });
          return cursor;
        }
      },
    ]
  });
}

if (Meteor.isClient) {
  var sub = Meteor.subscribe('topTenPosts', function () {
    console.log('top ten posts count', Counts.get('topTenPostsCount'));

    Posts.find({}).fetch().forEach(function (post) {
      console.log('comment count for post', post._id, ':', Counts.get('commentCountForPost' + post._id));
    });
  });
}

Don't underestimate the necessity to publish counts with unique names. Each time Counts.publish is called, a counter closure variable initializes to zero, and is published in publish-count's Collection, which means multiple subscriptions with dissimilar datasets will fight to store their count in the same Collection document.

In the example above, the count topTenPosts will survive multiple subscriptions (while wasting resources) because the cursor returns the same dataset, which evaluates to the same count every time. Keep this in mind when should you decide to add parameters to the top-level find, because you will need to use those arguments to produce a unique name.

If this answers your question, feel free to close the issue. Otherwise I'll leave it open for others to comment.

@boxofrox
Copy link
Contributor

boxofrox commented Aug 4, 2015

Recently updated the Readme and found that when returning cursors, noReady should be set. Example above updated.

@derwaldgeist
Copy link

This does not work for me. As soon as I add the Counts() call to the parent's find:, my publication fails. The reason is that the objects passed to the children function do not contain any content any more, they just have an _id, without any additional fields. If I remove the Counts() call from the parent, the children documents are OK again.

@boxofrox
Copy link
Contributor

boxofrox commented Nov 8, 2015

If you were using the same cursor for Counts.publish and Meteor.publish, then you ran into #58. And you're right, my example does just that. Updated.

@derwaldgeist
Copy link

Thanks, wasn't aware of #58!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants