Search code examples
meteormeteoritemeteor-publications

How to publish multiple collections in single subscription call in meteor?


I this possible to publish multiple collections in single subscription call? if so please guide me.


Solution

  • Yes. A publish function can return an array of cursors. For example:

    client

    Meteor.subscribe('roomAndMessages');
    

    server

    Meteor.publish("roomAndMessages", function (roomId) {
      check(roomId, String);
      return [
        Rooms.find({_id: roomId}),
        Messages.find({roomId: roomId})
      ];
    });
    

    important note

    If you return multiple cursors in an array, they currently must all be from different collections. We hope to lift this restriction in a future release.