Reputation: 656
Here is my big point : the users can visit different chatrooms. When they do so, I push the chatroom ID in a reactive array in the client, if the ID hasn't already been added. If it doesn't, I rerun an autorun that shall subscribe to the 50 last messages of every chatrooms the client visited in this session.
But I want him to be able to recieve only the 50 last messages for each chatroom !
Previously I had :
Meteor.publish("getChatMess", function(cids) {
if (!Array.isArray(cids))
cids = [cids];
return (ChatMess.find({ chatId: cids[i] }, { sort: { date: -1 }, limit: (75 * cids.length) } ));
});
Which obviously do not fit my needs, so I did that :
Meteor.publish("getChatMess", function(cids) {
if (!Array.isArray(cids))
cids = [cids];
let i = -1,
cursors = [];
while (cids[++i])
{
cursors.push(ChatMess.find({ chatId: cids[i] }, { sort: { date: -1 }, limit: 50 } ));
}
return (cursors);
});
But there, Meteor throw me an error, as I can't return several cursor from the same collection in a single puslish.
How can I solve that ? I know I could check for every chat, pick the ids of the 50 last messages (or less) of every chat and query the chatMess collection for all the corresponding ID, but it would be quite heavy and unoptimized for what I want, especially if the client has already visited dozens of chatrooms in the session.
Upvotes: 4
Views: 560
Reputation: 907
Just use
Meteor.publish("getChatMess", function(cids) {
if (!Array.isArray(cids)) cids = [cids];
return ChatMess.find({chatId: {$in: cids}});
});
Upvotes: -1
Reputation: 1726
As stated in meteor docs
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.
But there is a package called smart-publish which helps you to manage multiple cursors from the same collection. That might help https://atmospherejs.com/mrt/smart-publish
Upvotes: 2