How to publish multiple collections in single subscription call in meteor?
Asked Answered
A

1

9

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

Aleasealeatory answered 6/7, 2015 at 5:25 Comment(2)
Please edit your question and add a code snippet you tried to use. It would help people answering your question greatly! Read more about how to ask a great question.Idol
@methode. Thanks mate. I will followAleasealeatory
K
18

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.

Kirt answered 6/7, 2015 at 5:32 Comment(1)
Instead of returning an array, you also could create a "virtual" collection which you setup in your own publishing method. To filter those docs you are able to use arguments on publish/subscribe. If you just want to have complex documents to display content this may a smart solution. If you want to use the collection also for updates on db, an array result is mostly easier to implement. Cheers, TomTamar

© 2022 - 2024 — McMap. All rights reserved.