Meteor to share Mongodb with Flask App?


#1

Is it possible to share Mongodb app inside Meteor and allow another app, (Python-Flask for example) so that both applications can access the same database and collections?


#2

Yes! If you use the same DB, both should have read/write access to your database.
Realtime will still work with the oplog, but things like redis-oplog wont work. Another thing that won’t work: collection-hooks

These two won’t work because they add hooks to the Meteor collection, which don’t get called from flask (for obvious reasons: it’s not part of the meteor server).

And be very aware of your inserts/updates/deletes. Remember that Meteor is watching the database for changes (that’s how oplog works), so try to do as least operations as you can.


#3

Hi @fermuch

thanks! I didnt know this is possible. So say I have read/write in Meteor.js so that the dynamic MongodB changes from live editing in Meteor app occurs by user interaction to drag and place DateTime:

and another Python-Flask app to read only the changes and renders another HighChart data gantt chart. So in essense Meteor.js app doing the live edits, with Flask just doing the read. Doable correct?

Could you show how I can go about defining this in both?
I assume both need to reference the same Mongodb database/collection name…

But since they are launched at different ports? Just not sure how to set it up.

Reason I am doing this is because I cannot get dhtmlx to work in Python-Flask, if I could then I wouldn’t have to use this approach…


#4

Yes, it’s possible to do what you want, but it’s a terrible solution for the long run, since you’ll have to maintain two totally different systems for the same end goal.

You just need to use the same mongo credentials for both pymongo and METEOR_URL, and the same collections. One catch is that Meteor uses _ids as strings, so you’ll need to handle that. I’d recommend always using strings, since it’s a mess to use ObjectId and strings at the same time.

If you want your users to see both sites from the same address, you’ll need a proxy. For example you can configure nginx to redirect to port XXXX when the user goes to the main page, and to port YYYY when the user goes to /data.


#5

we have mulitple apps using the same DB. Locally just setup install mongodb so it can run outside of meteor, create the db and use the path to that db in your startup settings for each app. Setup mongodb to start automatically on startup makes things easier.

Now each app can talk to the same db instead of using the meteor local mongo instance.


#6

Hi @maxhodges, thanks for your comment. With Flask I need to setup mongodb via

$ brew services start mongodB

But with Meteor when I startup, it also automatically launches MongodB in the background, so are you saying that I somehow DON’T use or try to setup Meteor.js externally so that it is launched the same way that I launched Flask? If so how would you do that in Meteor so that it doesnt launch MongodB in the background? Or have I misunderstood your point?


#7

Meteor by default creates local mongodb database (projectname/.meteor/local/db) when you run:

$ meteor run

If you have mongodb already installed just run Meteor project with command:

$ MONGO_URL=mongodb://localhost:27017/meteorprojectname meteor run

If you don’t want to type this every time you run your project you can create a bash script:
$ echo 'MONGO_URL=mongodb://localhost:27017/meteorprojectname meteor run' > run.sh

additional details
http://meteor.hromnik.com/blog/meteor-run-without-creating-local-mongo-database

There are several ways to do it

in our case use have a startup script in package.json we call with

meteor npm run start

it looks like

  "scripts": {
    "start": "MONGO_URL=mongodb://localhost:27017/wreshared ROOT_URL=http://localhost:3000 meteor run --settings '.config/development.json' --port 3000",


#8

Hi @maxhodges why do I need this

.config/development.json

?

Since I have initially a database created in Flask-PyMongo named “test_jiraflask” and collection called “release_details”, like to know how Meteor.js would connect to this, so that it can render, update within Meteor.js dhtmlx-gantt chart.

So if my meteor projectname is “jirameteor”, then in the package.json:

“scripts”: {
“start”: “MONGO_URL=mongodb://localhost:27017/jirameteor ROOT_URL=http://localhost:3000 meteor run --settings ‘.config/development.json’ --port 3000”,

so now both the Flask and Meteor app are able to access it correct?


#9

You don’t need that part. He just showed how he is using package.json to simplify the call of meteor with custom arguments.

You can either call directly from your shell:

MONGO_URL=mongodb://localhost:27017/jirameteor meteor run

OR add to your package.json inside your scripts section:

{
  "scripts": {
    "start": "MONGO_URL=mongodb://localhost:27017/jirameteor meteor run"
  }
}

With package.json, you can execute npm start in your shell, and NPM will call the script you defined inside start.


You should replace jirameteor with your database name.

To use collections, you need to define the collections with the same name as you have in Flask.
For example, if you have a collection in flask named messages, you’ll need to add in your meteor code:

const Messages = new Mongo.Collection('messages');

Then you can call Messages.find({username: 'fermuch'}) or whatever query you want.


EDIT: I think this part is obvious, but just in case: in the url mongodb://localhost:27017/jirameteor, localhost is the address, 27017 is the port, and jirameteor is the database name. If you’re using a mongo server with the default config, that address (your computer) and port (the default mongo port) should work.