meteormeteor-publications

Meteor publish overwrites another publish


I have two publish method as below but when I subscribe to one of the publish method in client search page, it is being overwritten with the other one which is meant for index page.

Server

Meteor.publish("task.index", function() {
  TaskCollection.find()
}

Meteor.publish("task.index.search", function(state) {
  TaskCollection.find({ state: state })
}

Client - search page

Meteor.subscribe("task.index.search", state)
// this will always be overwritten with "task.index" published collection

Client - index page

Meteor.subscribe("task.index")

Does anyone know how to avoid this?


Solution

  • Welcome to SO!

    There is a high chance the "override" you see is just the normal Meteor behaviour for Publish/Subscribe mechanism.

    Your "task.index" publication sends all your TaskCollection documents to the Client.

    Therefore any other publication on that same TaskCollection will send documents that the Client already knows.

    Then in your Client, filtering some documents from TaskCollection is independent from your subscription and publication. Just perform your TaskCollection.find({ state: state }) Client side, and you will get the documents you need.

    When you publish only a subset of documents of a Collection, it happens that what you publish is exactly already the filtered documents that you want to display on your Client, therefore on your Client you just display all Collection documents you know of. But you have to understand that these are 2 different steps:

    1. Subscription to send some documents to the Client. Several Subscriptions may be set, filling the same Collection on the Client.
    2. Filtering on the Client, based on the documents sent by the (possibly several) Subscription(s).

    See also: Publish subscribe doesn't seem to work