2 ways to share data between 2 Different Meteor apps

For my next project, I have been delaying the decision on whether to use node.js, or an app framework like meteor that is built on top of node.js. While meteor seems like a pretty solid framework and getting all the buzz, the 2 factors that have held me back from joining the meteor bandwagon has been a lack of support for relational databases (like SQL), and its lack of adoption of standard package management for node development, npm. With the advent of meteor 1.3, the second reason for holding back on meteor is no longer an issue.

The apps that I develop frequently share a common database. So the first question for me after running through the standard Todos tutorial is to find out how to share data between 2 different apps and make both apps reactive to data changes. This is different from data reactivity support with multiple instantiation of the same app.

In meteor, the easiest way to accomplish this according to the meteor guide is to point both apps to the same database with MONGO_URL. Turns out, that is not sufficient for reactivity. You will also need to do oplog tailing as well so that your secondary app will have reactive data support.

Here are the steps on how to do this on a localhost Mac OSX dev environment.

  1. On your primary app, most google info out there says to run `meteor mongo -U` to get the correct MONGO_URL to configure.
$ meteor mongo -U
mongodb://127.0.0.1:3001/meteor

2. OK. Except your MONGO_URL configuration should have the form

mongodb://<host_address>:<port_number>/<db_name>

Using the simple-todos tutorial as an example, the correct MONGO_URL value on a localhost with the meteor app using the default port number 3000 is:

mongodb://127.0.0.1:3001/simple-todos

3. On your secondary app, you have 2 ways to tell it to retrieve data from the same database configured for your primary app.

a) Configure MONGO_URL, and MONGO_OPLOG_URL for oplog tailing in the environment. Using bash, you can do the following:

$ MONGO_URL="mongodb://localhost:3001/simple-todos";MONGO_OPLOG_URL="mongodb://localhost:3001/local"  meteor --port 5000

b) Or, if you prefer to do this programmatically, write this for your secondary app.

let driver = null;
if (Meteor.isServer) { 
driver = new MongoInternals.RemoteCollectionDriver(
“mongodb://localhost:3001/simple-todos”,
{
oplogUrl: “mongodb://localhost:3001/local”
});
}
export const Tasks = new Mongo.Collection("tasks", {_driver: driver});

As noted in the meteor guide, an export statement must be at the top level of your code and not inside and if-statement.

Happy coding…

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.