0

I am trying to implement a commenting system in a huge app and always run in the problem about cross reactiveness and publications.

The specific problem:

When a user writes a comment, I want to show the user's name and a profile picture. The comments are in one collection, the names and pictures in another.

When I make a subscription for every comment on this page and for every user whose id is in a comment of this page serversided, the app does not update the users available on the client when a new comment is added because "joins" are nonteactive on the server.

When I do that on the client, i have to unsubscribe and resubscribe all the time, a new comment is added and the load gets higher.

what is the best practise of implementing such a system in meteor? how can i get around that problem without a huge overpublishing?

Tobi
  • 1,175
  • 1
  • 19
  • 44
  • possible duplicate of [Meteor.publish: publish collection which depends on other collection](http://stackoverflow.com/questions/26398952/meteor-publish-publish-collection-which-depends-on-other-collection) – David Weldon Feb 04 '15 at 23:03

1 Answers1

0

As there is not official support for joins yet,among all the solutions out there in community

I found https://github.com/englue/meteor-publish-composite this package very helpful and I'm using it in my app.

This example perfectly suits your requirement https://github.com/englue/meteor-publish-composite#example-1-a-publication-that-takes-no-arguments

Meteor.publishComposite('topTenPosts', {
    find: function() {
        // Find top ten highest scoring posts
        return Posts.find({}, { sort: { score: -1 }, limit: 10 });
    },
    children: [
        {
            find: function(post) {
                // Find post author. Even though we only want to return
                // one record here, we use "find" instead of "findOne"
                // since this function should return a cursor.
                return Meteor.users.find(
                    { _id: post.authorId },
                    { limit: 1, fields: { profile: 1 } });
            }
        },
        {
            find: function(post) {
                // Find top two comments on post
                return Comments.find(
                    { postId: post._id },
                    { sort: { score: -1 }, limit: 2 });
            },
            children: [
                {
                    find: function(comment, post) {
                        // Find user that authored comment.
                        return Meteor.users.find(
                            { _id: comment.authorId },
                            { limit: 1, fields: { profile: 1 } });
                    }
                }
            ]
        }
    ]
});

//client

Meteor.subscribe('topTenPosts');

and the main thing is it is reactive

Sasikanth
  • 3,045
  • 1
  • 22
  • 45