Understanding publish with argument


#1

I am having trouble understanding the use case of publish with argument.

This is what i have currently in publication

Meteor.publish('stocks', () => {
  return Stocks.find();
});

and in the client side

export default createContainer((props) => {
  let subscribe = Meteor.subscribe('stocks');
  let stocks = [];
  if (subscribe.ready()) {
    console.log("subscribe,", subscribe)
    stocks = Stocks.find(
      {
        $and: [{ History: { $exists: 1 } }, { 'History.status': { $nin: ['PROCESSED', 'COMPLETED'] } }, { $where: 'this.History.length > 0' }]
      },
      { sort: { 'History.startDate': -1 } }
    ).fetch();
  }
  return {
    stocks
  }
}, RentalPage);

I am having a issue trying to render this page ( i am new to meteor but have completed projects in react without meteor).
There are around 2000 TOTAL stocks docs in the backend, and the query seems to take around 3-4 seconds (which i think is a lot) to Retrieve only 13 documents.

Now my question is if i use the following

Meteor.publish('stocks', (params,ctx) => {
  return Stocks.find(params,ctx);
});

and in the client code:

  let subscribe = Meteor.subscribe('stocks', {
        $and: [{ History: { $exists: 1 } }, { 'History.status': { $nin: ['PROCESSED', 'COMPLETED'] } }, { $where: 'this.History.length > 0' }]
      },
      { sort: { 'History.startDate': -1 } });
//some codes here 
stocks = stock.find().fetch()

Can the server do the calculation and send me the final docs (13) and i can just use it ? or this is not a correct way to do ? i am just confused using parameters during subscription.

Any suggestions are highly appreciated :smiley: :cheese:


#2

Let’s see, there is two issues here. One is data send from the server to the client and the second is the processing load .

If you publish the whole STOCK you are sending the 2000 docs to the client. You can check this if you install the meteor tools in Chrome. As any of your visitors can see them too. If you only publish the 13 docs you need, the client will only receive the 13 published docs and nothing else.

If you publish the whole STOCK the server will retrieve it all and send all the data to the minimongo on the client’s browser. The sorting and filtering will be done on the clients computer. But if you have proper indexes on your DB specific to this query, the server will fetch the 13 docs much faster and only serve this 13 docs.


#3

Thank you so much for the reply. so i see doing

Meteor.subscribe('stocks', {
        $and: [{ History: { $exists: 1 } }, { 'History.status': { $nin: ['PROCESSED', 'COMPLETED'] } }, { $where: 'this.History.length > 0' }]
      );

will Retrieve only 13 docs from the server and i can sort or filter on the client after
stocks = Stocks.find().fetch()

Sorry just trying to understand.Thanks in advance. :smiley: you rock


#4

Yes. Best is to test it yourself installing the Meteor DevTools extension in Chrome.


#5

So yes i tried it and it works. But i found that i needed to add the condition to subscirption as well as in the stocks.find().fetch()

  let subscribe = Meteor.subscribe('stocks',{
    $and: [{ History: { $exists: 1 } }, { 'History.status': { $nin: ['PROCESSED', 'COMPLETED'] } }, { $where: 'this.History.length > 0' }]
  });

//some codes

//this is after subscrition ready state ()
    stocks = Stocks.find(
      {
        $and: [{ History: { $exists: 1 } }, { 'History.status': { $nin: ['PROCESSED', 'COMPLETED'] } }, { $where: 'this.History.length > 0' }]
      },
      { sort: { 'History.startDate': -1 } }
    ).fetch();

There is no issue or harm in doing this but is this correct? do i need to put the conditions on both?

If i do not add condition the second time, it is actually pulling 256 docs (dont know why only 256 ? cause there are around 2k records)


#6

You should not need to duplicate it.
Are you passing the parameters correctly to the server?

Are the parameters going to change or are being hardcoded on the subscription? If they are not going to change I’ll put them directly to the publication in the server.

I’m afraid I don’t know why you are getting only 250 docs of the 2000 when you should only get the 13.


#7

Have a read of this, which explains much better than I can about the options and relationships between publications and subscriptions in Meteor:


#8

Thanks for sharing ! i now understand how to ! thanks a lot :slight_smile: