Search code examples
meteormeteor-publications

Implement added, changed and removed server side


Context : I am using a Collection Params to call method from the Server to a C app. The C app does its stuff and then calls the server by RPC to send me the results. With the result, I get the Params ID to delete the corresponding element. With the deletion of the Element of Params, the C app gets a removed message. I want to prevent this behavior to avoid overloading the C app of messages.

I've thinked about implementing the removed event into the Publish method on the server to prevent the server from informing the C app. I just want the C app to be inform about added events.

On the Meteor Doc, there is an example of implementation of added and removed but I don't understand it. Can someone help me ?

I've tried this (don't work at all) :

Meteor.publish('expert_mode_parameters', function ()
{
  var self = this;

  var handle = Expert_Mode_Parameters.find().observeChanges({
    added: function ()
    {
        return Expert_Mode_Parameters.find();
    },
    removed: function ()
    {
        return [];
    }
  });

  self.ready();

  self.onStop(function () {
    handle.stop();
  });
}

Solution

  • It looks like your goal is to subscribe to a data set but only receive added messages, not changed or removed.

    The below code should do this:

    Meteor.publish('expert_mode_parameters', function () {
      var self = this;
    
      var handle = Expert_Mode_Parameters.find().observe({
        added: function (document) {
          self.added("expert_mode_parameters", document._id, document);
        }
      });
    
      self.ready();
    
      self.onStop(function () {
        handle.stop();
      });
    }
    

    The concept is, you're watching the results of Expert_Mode_Parameters.find() and then calling self.added(document) when there is a new item. The same thing can easily be expanded to include changed.