Inserting a record to collection, but don't want to publish all records for subscription

I’m building a simple feature where a user can create a “Community” similar to Facebook’s group.

I’m having trouble wrapping my mind around what to publish from the server so my React component can subscribe to it. Do I even need to publish the Community collection?

I can’t publish all the Communities because it will be a huge list of every community ever created on the site.

// RIP my server.
Meteor.publish('communities', function() {
  return Communities.find();
});

What do I publish from the server so my component can subscribe to it? Any suggestions?

1 Like

Changed the title so it’s a bit clearer.

It is up to you what you publish. For example, assuming every community record has a userId, you could easily publish only the communities by the logged-in user

Meteor.publish('communities',function(){
 Communities.find({userId: this.userId});
});

another way would be to actually pass parameters from your subscribe to the publish

Meteor.subscribe('communities', 'A', 'B', 'C');

Meteor.publish('communities', function(a,b,c){
 // create query from parameters and return results ...
});

I recommend you read the Meteor documentation and/or look into the examples to shed light on the pub/sub flow.

Oh yeah that makes sense, but in this case I don’t really want to publish any communities. I just to create a brand new community.

Do I still need to publish something?

You only need to publish from the server what you want available on the client (minimono and reactivity).

If you just want to insert a document into a collection, you only need a method.

1 Like

Thank you, that answers my question. :slight_smile:

Some of my React components don’t really need to load any data, just save it. So I will just call the method like you suggest.