bvs
bvs

Reputation: 1087

how to configure mongodb for use with meteor (was: how to make meteor reactive to direct mongod changes)

Original question below so that first answer makes sense. But it turns out the real problem is that you need to configure mongodb for use with meteor if you create the db yourself instead of letting meteor do it.

Original question:

I'm building a meteor app that displays a list of items. If I add or delete entries to a published list using the HTML app it is instantly reactive across all browser instances. If however, I make a change to the database using the direct MongoDB API the browsers do not update right away. (My actual scenario is for these entries to be published from a data source, not from the browser.)

The question is what do I need to do in this separate app, which is inserting data into mongo so that the meteor app sees the changes immediately.

Upvotes: 1

Views: 2230

Answers (2)

bvs
bvs

Reputation: 1087

I never would have figured this out without Jermey's answer and the comment about oplog. In the end everything he said was true. The problem was that I had created a single node mongdb instance without turning on oplog. However, meteor requires that oplog be enabled to for reactivity to work properly.

Detailed descriptions of turning on oplog can be found at https://loosexaml.wordpress.com/2012/09/03/how-to-get-a-mongodb-oplog-without-a-full-replica-set/ or https://themeteorchef.com/tutorials/setting-up-mongodb-oplog-tailing.

But here is a short self-contained version for Ubuntu:

First edit /etc/mongodb.conf and add

replication:
  replSetName: rs0
  oplogSizeMB: 100

Then connect to the database with mongo and type

use local
rs.initiate()

Then before launching the meteor app set MONGO_URL to point to the remote DBS and set MONGO_OPLOG_URL to point to the local DBS on the remote. For example:

export MONGO_URL="mongodb://mongohost:27017/simple"
export MONGO_OPLOG_URL="mongodb://mongohost:27017/local"
meteor run

Upvotes: 3

Jeremy Iglehart
Jeremy Iglehart

Reputation: 4479

So long as you are:

  1. Properly publishing the data
  2. Are properly subscribed to the data
  3. Your templating engine (Blaze, React) is displaying the data from the output variable in a reactive context

Everything should be fine. Reactive everywhere - even from a database update. This is the default behavior from Meteor. Everything should just work.


To be extra clear: You don't need to do anything to your other app which accesses the MongoDB.

You could be editing the MongoDB by hand from the command line - and so long as you've hooked everything up like I've illustrated above - everything will work as if somebody put the data in using your Meteor App. A MongoDB is a reactive source in Meteor, and because of Meteor's Tracker, Meteor will serve this data up reactively no matter how the database changes. This is the default behavior.


Show us your code:

If you share the code in question either here or as a link to Github I will edit my answer to point out why your code is not doing this.


More reading:

I highly suggest that you take a moment and read the entire tracker manual (I have). This explains how Meteor "Reactivity" works and should teach you everything you need to know as to why your code isn't working. It's actually quite an easy read, just a long one. If after reading that, it is still not clear - I would be happy to have a skype conversation with you and explain what is happening in your code line by line. What you're trying to do is actually quite easy in Meteor.

You can also go read here to see how MongoDB's oplog communicates with Meteor to provide that crispy reactivity you're looking for. As stubilo said in a comment a few moments ago. If your MongoDB doesn't have an oplog setup, then meteor falls back to polling (which is obviously going to be slower). (Thanks stubilo)

Upvotes: 4

Related Questions