Connecting two Meteor apps via DDP.connect requires the following steps (to my knowledge):
- Publish the collection on the remote app
- Connect via DDP.connect to the remote app
- Subscribe to the remote collection (same object that was returned via DDP.connect)
- Create a new Mongo collection
- Return the docs of the collection in the helper file
- Displaying the docs in the template (html file) with {{each collectionName}} {{/each}}
I have two apps (one remote on that is connected directly to the MongoDB and one much smaller one that is handling all the frontend interactions). I can successfully manage to connect them via (step 2 & 3 - the code is in the lib
folder in a file called _connect.js to ensure it's executed before any other code in that folder):
if (Meteor.isServer) {
if (process.env.NODE_ENV === 'development') {
// development environment
worker_host = 'localhost:4000'; // ip address of job worker host
// connect to job worker
remote = DDP.connect(worker_host);
}
remote.subscribe('cards', function() {
console.log('Number of docs: ' + Cards.find().count());
});
}
and it also displays the number of expected docs when I then use remote.subscribe
. This is the only place in the code where it shows the correct number of docs, everywhere else it shows 0.
The cards collection is properly published on the remote server (which is running on port 4000) via (step 1):
Meteor.publish('cards', function() {
return Cards.find()
});
Step 4: I'm unsure where exactly to place this code. It's currently in the lib
folder so that the collection is known both on client and server:
Cards = new Mongo.Collection('cards',remote);
console.log('Number of docs: ' + Cards.find().count());
It always shows 0 docs when this code is executed.
However when I want to access the same (remote) collection in the template helper (step 5 - file is in client
folder):
Template.puzzle.helpers({
cards: function() {
console.log('Number of docs: ' + Cards.find().count());
return Cards.find();
}
});
Not a single doc is found in the helper code.
For the above reason nothing is displayed in the html template (also in client
folder):
<template name="puzzle">
<div class="container">
{{#each cards}}
// lot's of <div> displaying the content of the cards doc
{{/each}}
</div>
</template>
Obviously I must have overlooked something very simple, I suspect it's where my relevant code is being placed.
What steps of the above code do I need to change to access the remote collection? I've checked the usual sourced (SO, Meteor forum) but can't find the recommended structure for connecting two apps.
PS: I've read Connect two Meteor applications using DDP