Multiple publications of the same collection best practices

So everything works great when you publish only one set of documents per collection. But what if you need to publish a multiple sets?

For example, let’s say we have a collection called Records. We can publish either latest, most rated, most viewed records depending on the page user’s visiting. But in most websites it’s a common practice to put a sidebar with quick links to other sets of data, like related records, on almost any page.

So I need that related records sidebar appears on each and every page. That leads us to publishing at least two sets of documents for single collection Records. But in that case I’ll need to duplicate queries to that collection on client. In case we had only one publication I could just select documents I need on server and on client just do Records.find({}). With multiple sets of the same collection that trick won’t work.

It’s fine when we have some simple publication with a few fields in query. But things is starting to be really nasty when there’re complex queries with regexes, $and, $or and in my case $text operators.

There’s a simpler solution for this case I’m thinking of. It’s not use Meteor.publish but instead use Meteor.methods to get a set if objects to display in sidebar. This won’t be a publication so I’ll have only one publication of single collection on client and I won’t be needed to duplicate collection query on the client. But using that solution kills reactivity.

So what’s the recommended way to solve such common task?

3 Likes

Minimongo and the publish/subscribe architecture is designed so that you are expected to duplicate the query on the client. The client mirrors Mongo’s document storage in order to minimize the amount of data that needs to be sent to each client.

Meteor allows code to be shared between client and server though, making it possible to use the same find() query cursor from a single Javascript file so you do not have to repeat any code.

Ok I get it. But what about $text query operator? Does Minimongo support it?

1 Like

I know, this topic is pretty old, but I guess that this StackOverflow post worth being mentioned. It explains how to manage multiple publications and subscriptions on the same collection and I suppose what stated is still absolutely valid.

6 Likes

Does anyone know if you can have two subscriptions on the same document, but have one subscription return a few fields reactively and have another subscription return a certain other field(s) using polling - like a counter field for example? So the polling subscription would use disableOplog: true, pollingIntervalMs: 3000?

I have a use-case where I need most fields of a document reactively, but one field is a counter that gets an $inc thousands of times a second (sometimes) and when this happens it freezes Blaze and my entire HTML UI (I have animating timers and things going). I can change the overall subscription to use disableOplog: true, pollingIntervalMs: 3000 and this fixes the problem but that leads to clunky UI on the other fields I want to work reactively. Was curious if you could blend the two?

Going to try it and see what happens.