Meteor related subscriptions in React

getMeteorData() {
const recentGroupSubs = Meteor.subscribe(‘recentGroups’);

if (recentGroupSubs.ready()) {

  // get published groups ids
  const groupIds = _.pluck(Groups.find().fetch(), '_id');

 // subscribe for only found group messages
  
  Meteor.subscribe('messages', groupIds);

}
}

Obviously this will cause recursive loop. But what i mean by this is how can i solve this kind of (related subscription) problem.

Don’t pass groupIds to the server, just get them in your messages subscription. I’m not sure this would be reactive though, so think about using publish-composite.

@fvg thanks for your reply. I know publish-composite. This is a example code to demonstrate my question. In my real code my publication code is quite big. So i thought that i should split one big publication to 2 small ones. And I am just curious that is this possible to solve this kind of problem in react component.

1 Like

I was on the phone before and didn’t really have a look at your code. Without trying I don’t see any problems with it. What about something like this?

getMeteorData() {
  var groupsSub = Meteor.subscribe('recentGroups');

  var data = {
    groups: Groups.find().fetch(),
    groupsLoaded: groupsSub.ready(),
    messages: [],
    messagesLoaded: false
  };

  if (data.groupsLoaded) {

    // get published groups ids
    var groupIds = _.pluck(data.groups, '_id');

    // subscribe for only found group messages    
    var messagesSub = Meteor.subscribe('messages', groupIds);

    data.messages = Messages.find().fetch();
    data.messagesLoaded = messagesSub.ready();
  }

  return data;
}

(CoffeeScript user, so I removed all consts :wink:

@fvg thanks. It is working