Obiwahn
Obiwahn

Reputation: 3087

How to only subscribe to database updates (without fetching)?

I have a large database and I only want to send actual data updates to the client.
I do not want each client to fetch the whole set of data as it is done natively with Meteor's pub/subs.
I tried ground:db but it still fetched all data.

How can I achieve this?

Upvotes: 2

Views: 75

Answers (2)

Kyll
Kyll

Reputation: 7151

To achieve this you can observeChanges on your whole collection and channel the changes (added, changed, removed) to each publication handler.

A very rough application would look like this :

var pubHandlers = []

Meteor.publish('changes stream', function handlePublication() {
  pubHandlers.push(this)
  this.onStop(() =>
    pubHandlers.splice(pubHandlers.indexOf(this), 1)
  )
  this.ready()
})

myCollection.find().observeChanges({
  added : function docAdded(id, doc) {
    for(let pubHandler of pubHandlers) {
      pubHandler.added('my collection', id, doc)
    }
  },
  changed : function docChanged(id, fields) {
    for(let pubHandler of pubHandlers) {
      pubHandler.changed('my collection', id, fields)
    }
  },
  removed : function docRemoved(id) {
    for(let pubHandler of pubHandlers) {
      pubHandler.removed('my collection', id)
    }
  }
})

Note that in that case docRemoved will throw if the document didn't exist on the client.
In that case, all updates on your collection will be sent to all subscribed clients. You may want to use some smartly distributed Mongo.Cursor to share the load in a cleaner way.

Upvotes: 1

KG32
KG32

Reputation: 150

How about cursor.observeChanges?

http://docs.meteor.com/#/full/observe_changes

Upvotes: 0

Related Questions