1

Connecting two Meteor apps via DDP.connect requires the following steps (to my knowledge):

  1. Publish the collection on the remote app
  2. Connect via DDP.connect to the remote app
  3. Subscribe to the remote collection (same object that was returned via DDP.connect)
  4. Create a new Mongo collection
  5. Return the docs of the collection in the helper file
  6. 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

Community
  • 1
  • 1
a4xrbj1
  • 445
  • 3
  • 21
  • So 10 seconds after I post I'm getting a "This question doesn't show any research effort or it is unclear or not useful"? How can you speed read in 10 seconds my whole post, including the last line which clearly shows that I've researched this topic? I can't structure my code any better. If you don't want to provide any help then just move on but don't downvote randomly without even properly reading a help request. – a4xrbj1 Jan 28 '16 at 05:58
  • 1
    You need to decide where you want to connect from. Server or client. As you shown in code, you are defining `remote` only on server, so DDP, new Collection("name", remote) and subscribe is possible only on server. And that is where your data are available. If you want to have them on client - created DDP and subscribe directly to that 2ndary server from client. If you want it to be first to server and than publish it to client - you will need Client side collection and than publish from server side with probably low level API I think. – Robert Simon Jan 28 '16 at 06:18
  • Maybe if you create that Collection on client side normally, without remote. Or with null to be client side only, but keep the name same, you will be able to publish it without low level API, but I never needed such case. Good luck testing it. – Robert Simon Jan 28 '16 at 06:25
  • Thanks @RobertSimon , as always your help is much appreciated. Will try your suggestions. The collection data is for sure needed on the client to display it on the frontend. I will refactor the rest of the application to use only Meteor.call on the remote server to alter the collections on the remote server. That way the connection needs to moved to the client as per your comment. So despite Meteor's ability the frontend app won't have any backend part (that is solely on the remote app). – a4xrbj1 Jan 28 '16 at 07:59

1 Answers1

0

The correct way to do this is indeed what @Robert Simon has suggested, make a decision to either run it on the server only or on the client only. As I need to display the contents of the collection via the client, everything had to be moved to the client.

These are the edits that made it work:

Step 2/3 (moved the _connect.js file to client folder:

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 cards1: ' + Cards.find().count());
});

Step 4 (moved the cards.js file to client folder:

Cards = new Mongo.Collection('cards',remote);

When the puzzle.js file (the helper) is executed the first time it's not displaying any cards as the app runs through it so fast that no transfer of data via DDP has happened. On a subsequent second run all docs are received in the helper and get displayed in the html file.

a4xrbj1
  • 445
  • 3
  • 21