Call frontend methods from external meteor application
Asked Answered
L

2

10

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!

Lade answered 8/5, 2015 at 5:44 Comment(0)
R
1

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.

Rachaba answered 12/5, 2015 at 20:19 Comment(4)
The issue is that when I do this, I get a TypeError saying that "registrationServices.loginWithFacebook" is undefined. On the backend Meteor app (for registrationServices), there is only a server folder and no client folder - could this be the issue?Lade
No, in this case you should not need a client folder. Basically in my example I called it like a client side method. It must be called as a server side method. I will update the answer in a bit with better syntax but basically you'll want to use registrationService.call('loginWithFacebook',...);Rachaba
There you go, I have updated the example to work with the server side call a bit better. Let me know how that works out.Rachaba
I think the issue is that 'loginWithFacebook' is a client side method, and DDP.call will invoke the server-side methods only.Lade
B
1

You can maybe achieve connection through your other service by specifying the service's remote connection to Accounts and Meteor.users:

var RegistrationService = DDP.connect(registration_service_url);
Accounts.connection = RegistrationService;
Meteor.users = new Meteor.Collection('users',{connection: RegistrationService});

Then would call Meteor.loginWithFacebook and it should use the other app's methods for logging in.

Bearnard answered 13/5, 2015 at 9:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.