Sorry if this has been covered in many disparate places, but I think I’ve crawled the entire internet and still can’t figure out the last part of creating a second Meteor app to connect to the first app’s db.
Scenario: there is a consumer-facing app “App A”, and now I’m creating an Admin app “App B” that connects to app A. Let me share code to illustrate the issue (please note I’m omitting some code that handles performance/paging/security so that only the relevant code is shown):
App A lives on localhost:3000 and is started up with: meteor
App B lives on localhost:4000 and is started up with: MONGO_URL="mongodb://127.0.0.1:3001/meteor" meteor --port 4000
// App A: /imports/api/server/pubs.js
Meteor.publish('matrixData', function () {
var user = Meteor.users.findOne({_id: this.userId});
console.info('matrixData', user._id, user.isAdmin, matrixData.find({}).count());
if(user && user.isAdmin) return matrixData.find({});
});
// App B: /imports/api/startup/both/index.js
import { Meteor } from 'meteor/meteor';
import { Accounts } from 'meteor/accounts-base';
import { DDP } from 'meteor/ddp-client'
var remoteUrl = 'http://localhost:3000';
Meteor.remoteConnection = DDP.connect(remoteUrl);
Accounts.connection = Meteor.remoteConnection;
Meteor.users = new Mongo.Collection('users', {
connection: Meteor.remoteConnection
});
// __meteor_runtime_config__.ACCOUNTS_CONNECTION_URL = remoteUrl;
// ^^ tried this from a post jamgold posted, but commented out for now. doesn't seem to make a difference. :( ```
// App B: /imports/api/collections.js
import { Meteor } from 'meteor/meteor';
import { Mongo } from 'meteor/mongo';
export var matrixData = new Mongo.Collection('matrixData', {
connection: Meteor.remoteConnection
});
And relevant bits in matrix.js:
// App B: /imports/ui/matrix.js
import { Meteor } from 'meteor/meteor';
import { Template } from 'meteor/templating';
import {
matrixData
} from '/imports/api/collections.js';
import './matrix.html';
Template.Matrix.onRendered(function(){
Meteor.remoteConnection.call('isAdmin', function(err, res){
if(res){ // method returns boolean
Meteor.remoteConnection.subscribe('matrixData');
}
}
});
Template.Matrix.helpers({
mData: function(){
var data = matrixData.find({});
console.info(matrixData.find({}).count()); // always 0!
return data.count();
}
});
Being an Admin app, App B requires authentication.
// App B: /imports/ui/home.js
Meteor.loginWithPassword(email, password, function(err, res) {
if (!err) {
FlowRouter.go('matrix');
}
});
Above works great on App B and I can see the login event fires on App A’s side. (One issue is that once I’m logged in, a reload kicks me out and I’d love it if anyone can provide insight into how to keep the authentication persistent across browser reloads.)
On login to App B, I’m redirected to the matrix view. Then Meteor.remoteConnection.call('isAdmin'
successfully fires, which performs security checks on App to be sure I’m a privledged Admin. If I am, the subscriptions begin.
Here’s the strange part: in App A (/imports/api/server/pubs.js
) above, you can see I’m console logging some information. When the subscription to matrixData
fires, I can successfully see this in App A’s server-side console:
'matrixData' '8fKtRBPXZis2ZrZm2' true 39
So it seems the subscription is clearly being subscribed to on App B’s side, and the “39” is the number of documents in that collection. But on App B’s side, I get zero documents every time. I’ve tried to disable browser security policies, I have zero pubs defined on App B as well as zero Methods on App B.
Can anyone spot what I’m missing? Why is App A showing I’m subscribing to a Collection with N documents, but App B can’t get even a single document from the same Collection?