Can you connect to a separate meteor application with DDP?

So, small example. Make two different leaderboard examples. Change one of them to add the following, and also add grigio:babel and alanning:roles and nimble:restivus

// server/publications/players.es6
Meteor.publish('players', function () {
  if Roles.userIsInRole(this.userId, 'view') {
    return Players.find();
  }
  return [];
});

// client/templates/players.es6
Template.leaderboard.onCreated(function () {
  this.autorun(() => {
    let subscription = this.subscribe('players');
  });
});

Okay, now on the other application, if one is running on http://localhost:3000 and one is running on http://localhost:3030, is it possible to create a connection between the two and get the players subscription?

Normally, the way to go is DDP.connect() but your subscription requires a logged in user, therefore you’d need to read through Connecting 2 apps over DDP, how to login across? to see what your options are, and you do have a couple of options, each with their own pros and cons.

3 Likes

Please check DDP documentation http://docs.meteor.com/#/full/ddp_connect will surly fullfill your need. :smile:

2 Likes

Could this theoritically work?

Template.loginForm.events({
  'click button[type="submit"]': function (e, template) {
    let $form = $(template.find('form'));
    let remote = DDP.connect('http://localhost:3030');
    var credentials = {
      user: $form.find('input[data-key="user"]').val(),
      password: $form.find('input[data-key="password"]').val()
    }
    remote.call('login', credentials, function (err, userId) {
      remote.setUserId(data.userId);
    });
  }
});

Kind of.

First of all, the user part of the credentials should be an object like user: {username: "..........."}

Also, this would send the password in plain text. So if you were to use this setup on the internet, rather than localhost, you would have to use SSL.

Check this excellent SO answer for a full solution:

This appeared to work, is it secure?

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

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

It allowed me to do this:

Template.login.events({
  'submit #login-form': function (e, template) {
    let user = $(e.target).find('input#email').val();
    let password = $(e.target).find('input#password').val();
    Meteor.loginWithPassword(user, password, function () {
      FlowRouter.go('/dashboard');
    })
  }
});
2 Likes

Yes, this means:

“I want my meteor app to have its own local server, but use the users from another meteor server”

So it is as secure as a normal meteor app can be.

Always nice to have SSL in place, though :wink:

But can you use collections from another meteor app? I tried:

Collection = new Mongo.Collection('orders', {
  connection: DDP.connect('http://localhost:3030')
});

‘orders’ exists on the other application

Yes, that’s correct.