How to get the client IP inside a Route

148 views Asked by At

I'm trying to retrieve the customer's IP address during Iron Router's routing process. I have a server-side function for this (getIP), however the "waitOn" function inside the route won't wait for the server function to return:

waitOn: function () {
        Meteor.call('getIP', function(error, clientIp){...}}

Can I force it to wait, or can I get the IP in any other way?

1

There are 1 answers

0
Ricardo Pesciotta On

According to the documentation, the waitOn hook must return a Handler, a function or an array. the reason why it's not working for you, is that Meteor.call on the client is always async, and you have to define a callback function, which is called when the method responds. Given that nature, you could only use the Meteor method, if the waitOn code supported a Promise, that could be resolved on the method callback.

The only way I see this is the following:

  • use a Meteor.onConnection hook, and store the current IP Address of the user in the user's profile (Meteor.users collection)
  • setup a global Subscription that publishes the entire user profile (since by default Meteor.user only publishes a few default document fields).
  • on the route waitOn, query the Meteor.user collection, and you will see the current detected IP Address of that user

I hope this helps and that it works for you.