GithubHelp home page GithubHelp logo

isabella232 / meteor-control-mergebox Goto Github PK

View Code? Open in Web Editor NEW

This project forked from peerlibrary/meteor-control-mergebox

0.0 0.0 0.0 21 KB

Control mergebox of publish endpoints

Home Page: https://atmospherejs.com/peerlibrary/control-mergebox

License: BSD 3-Clause "New" or "Revised" License

JavaScript 7.03% CoffeeScript 92.97%

meteor-control-mergebox's Introduction

control-mergebox

UPDATE: This is now available in Meteor core.

This Meteor smart package extends publish endpoints with control of the mergebox for a given publish endpoint function.

Publish function's this is extended with this.disableMergebox() which when called will disable mergebox for current publish endpoint.

By disabling mergebox one chooses to send possibly unnecessary data to clients (because they already have it) and not maintain on the server side images of clients' data, thus reducing CPU and memory load on servers.

Server side only (with compatibility changes on the client side).

Installation

meteor add peerlibrary:control-mergebox

Discussion

This package disables storing the image of client's data on the server side for a given publish endpoint. This is useful to reduce CPU and memory load on servers, but it makes server unable to do some things it could do before:

Server does not know which fields client already has and what are their values, so if your publish function, for example, calls this.changed('collectionName', id, {foo: 'bar'}) twice in a row, server will not suppress sending the change twice. This is not so problematic because it keeps the semantics unchanged, just makes more data go over the wire.

Server also does not know which documents were published from which subscription. This is more problematic. With mergebox, Meteor tracks which subscription published which documents (with which fields) and if a document with same ID (and with possibly overlapping fields) is published from multiple subscriptions, Meteor knows what to do when one subscription removes a document (or a field) which still exists in other subscriptions. It removes just the fields previously published by this subscription only, while keeping all other fields for the document published. With disabled mergebox, when one subscription removes a document or a field that change is propagated immediately to the client as it is. This is different semantics to the one with enabled mergebox. So it is important to remember, with disabled mergebox, the last document or field change across all subscriptions is always the one propagated to the client side. There is simply no state in subscriptions anymore.

The default way to publish collections is to use observeChanges, explicitly or by returning a cursor from the publish function. This works well when server uses mergebox because only changes are really needed. But if you use multiple subscriptions over the same collection with disabled mergebox you might get strange results. For example, one subscription could remove a document (which would remove whole document on the client side) and then another subscription could change one field, which would result in document being re-added on the client side, but just with that one field. To improve this, one could use document-level observe:

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

  var handle = collection.find({}, {transform: null}).observe({
    added: function (newDocument) {
      self.added('testCollection', newDocument._id, _.omit(newDocument, '_id'));
    },

    changed: function (newDocument, oldDocument) {
      _.each(oldDocument, function (value, field) {
        if (!_.has(newDocument, field)) {
          newDocument[field] = undefined;
        }
      });

      self.changed('testCollection', newDocument._id, _.omit(newDocument, '_id'));
    },

    removed: function (oldDocument) {
      self.removed('testCollection', oldDocument._id);
    }
  });

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

  self.ready();
});

But the question is then how much overhead do you gain on the server by using observe instead of observeChanges, because observe has to cache documents.

In general it is advisable to not use multiple overlapping (in documents) subscriptions when disabling mergebox. Behavior is then predictable and matches the normal Meteor behavior.

  • meteor-streams โ€“ allows sending of messages from server to client without a mergebox, but then requires manual handling of those messages on the client, not using features already provided by Meteor

meteor-control-mergebox's People

Contributors

mitar avatar theodordiaconu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.