Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Call frontend methods from external meteor application

I am making a dockerized services-based application. Some of the services will be written in meteor, some won't.

One of the services is a registration service, where users can register for the platform.

When doing microservices, normally I do the following:

var MyService = DDP.connect(service_url);
var MyOtherService = DDP.connect(other_service_url);
var RegistrationService = DDP.connect(registration_service_url);

What I want to do is use the loginWithFacebook method. The issue is that using Meteor.loginWithFacebook on the frontend will invoke its backend methods on the main frontend server.

However, I want to invoke its backend methods on the RegistrationService server (which has the relevant packages). The reason is because I am using the Accounts.onCreateUser hook to do extra stuff, and also because I want to keep the registration service separate from the frontend.

Just for clarity, even though it is not correct, imagine I have this:

'click #facebook-login': function() {
  Meteor.loginWithFacebook(data, callback)
}

However, I want the loginWithFacebook method to use the server-side methods from RegistrationService when calling the client-side method .loginWithFacebook, so I actually want to do something to the effect of the following:

'click #facebook-login': function() {
  RegistrationService.loginWithFacebook(data, callback)
}

Any help on this will be greatly appreciated. Thank you!

like image 839
user2205763 Avatar asked May 08 '15 05:05

user2205763


1 Answers

I believe you are looking for DDP.connect. Basically underneath meteor all calls to the server from the client and all communication from the server to the client use Distributed Data Protocol. (https://www.meteor.com/ddp) As the documentation points out by default a client opens a DDP connection to the server it is loaded from. However, in your case, you'd want to use DDP.connect to connect to other servers for various different tasks, such as a registration services server for RegistrationService. (http://docs.meteor.com/#/full/ddp_connect) As a simplified example you'll be looking to do something like this:

if (Meteor.isClient) {
    var registrationServices = DDP.connect("http://your.registrationservices.com:3000");

    Template.registerSomething.events({
        'click #facebook-login': function(){
            registrationServices.call('loginWithFacebook', data, function(error, results){ ... }); // registration services points to a different service from your default.
        }
    });
}

Don't forget that you can also have various DDP.connect's to your various microservices. These are akin to web service connections in other applications.

like image 175
Tim C Avatar answered Oct 18 '22 02:10

Tim C