Connecting multiple apps using DDP for publish/subscribe usage only

Hi I’m trying to connect 2 Meteor apps.

App 1 is the main app with UI + auth + some collections.
App 2 is just a backend with a single collection.

I’d like App 1 to access App 2’s collection. I was able to do this and subscribe/publish using DDP. However it shows all the documents in the collection.

How do I authenticate client side on App2 so that I can apply filters to the publish to only show items owned by the logged in user?

Check out this thread.

What you suggest in that thread is to login to App1 then login to App2. For clarification does this mean I’d have to setup App2 with accounts-password package and configure it to point to App1 DB?

The workflow would look like, login to App1, login to App2, which redirects requests to App1 DB. So 2 hits to App1 DB?

Seems a bit messy to me…

What you suggest in that thread is to login to App1 then login to App2. For clarification does this mean I’d have to setup App2 with accounts-password package and configure it to point to App1 DB?

Yes. If you want to authenticate from the client you will need to have an auth DB associated with both connections. I had assumed you want the same authenticated user connected to App1 would do the connecting to App2 through their client? If so, then both apps would need auth support for the client connections.

An alternative is to connect server to server App1 <==> App2. Then you could write a publish function on App1 that fetched the data from App2 and would be able to filter for the user logged into App1. Then App2 would only need to connect securely to App1 and would not need to know anything about users.

App2 could have a publish function with arguments, the args being how to filter down the data.

App1 would validate the user then make a secure call to App2 (with filtering args) and then publish that collection to the user.

I’m trying this out but having trouble getting App2 (port 3030) to connect to App1 (port 3000) properly.

I temporarily added accounts-ui package and a view in App2 to test the login. I used the following code (client side)

Meteor.remoteConnection = DDP.connect('http://localhost:3000');
Accounts.connection = Meteor.remoteConnection;

Meteor.users = new Mongo.Collection('users', {
	connection: Meteor.remoteConnection
});

After I click sign-in and enter all the credentials. Nothing appears to happen (I still have the link to sign-in). In the console I can do Accounts.userId() and a correct value is returned. However this.userId() is undefined. Thoughts?

Hi @lucas,

Have a look at this: https://www.youtube.com/watch?v=DtCfRyJGlAk&t=977s. I found it very useful to set up the initial interfaces between clients & servers or server to server.

I recommend setting up a small test with a couple of simple servers and experiment with connecting them multiple ways.

Once you have that working then overlay your accounts strategy.

I assume this snippet is trying to log into app1 from app2 client? I assume you are using a shared DB (e.g. setting MONGO_URL)? Both App1 & App2 connect to the same Mongo instance?

Couple of observations

  1. The code you show, does not create an accounts client (e.g. new AccountsClient) and authenticate using it (e.g. Meteor.remoteConnection.loginWithToken). So there is no authentication on this connection.
  2. I think because you added the accounts package, your authentication is completing on the default connection for your app. When you started app2, which port do you set? 3030? (e.g. meteor -p 3030). If so, then your auth is likely completing on the default connection 3030 and Accounts.userId() would be working.
  3. this.userId is only available within a meteor method or a meteor publish function. It is primarily used to verify the user on the server. So when you check it at the client console, you need to be within a Method scope.

Once you set up the 2nd connection you will then need to subscribe on that connection in order to get data from that connection.

why not just have them both use the same database? we have three meteor apps which use the same database.

Thanks @brucejo. A lot of helpful information in this thread.

My App1 and App2 are on 2 different mongo instances.

I’ll keep poking around.

Hi @lucas,

Here is a possible solution:

  • Mongo DB for users only. used by both app1 & app2.
  • Mongo DB for app1
  • Mongo DB for app2

check out this stackoverflow: Using Multiple Mongodb Databases with Meteor.js. The thread claims you will lose oplog tailing, but I bet it can be configured.

That’s how I was trying to get it setup. Have trouble getting App2 to use App1 db for users collection. Seemed pretty straight forward but gave me a lot of frustrations so I’ve put it aside for now to save my sanity. Will revisit in the new year.

1 Like