Closed gadicc closed 10 years ago
My current need is that I'm calling a rest api on connection, the api takes the visitor's ip as a param. The results of the api are then displayed in the browser. Any suggestion for a workaround?
If I'm not mistaken, headers.methodClientIP(this)
should still work. Just rushing out now but I'll check back again a bit later. If it doesn't work, it won't take me long to make a new release where it will work... this data (as opposed to the original header and IP info) will be available already at connect time, although it's possible that my code doesn't realize this :)
Changed to headers.methodClientIP(this) and receiving the same error.
Ooh, it looks like the onConnection callback is called before Meteor has actually attached the session. But we can work around it quite easily. Hope following code will finally solve your problem :)
Meteor.onConnection(function(connection) {
var ip = headers.methodClientIP({connection: connection});
console.log(ip);
});
Hi Diyah, did this help you? I think this is an acceptable workaround and would like to close the issue if you're happy.
Sorry, for the delay in response. Yes, that works great!
Happy to hear it :)
How would you handle a proxy with this code?
at the proxyCount as a 2nd argument to methodClientIP(). in the workaround above, this would be:
var ip = headers.methodClientIP({connection: connection}, proxyCount);
even better, call headers.setProxyCount(proxyCount)
somewhere appropriate (in a lib/config.js file or your main site js or something). In the future, this will use HTTP_FORWARDED_COUNT environment variable, which was introduced in Meteor 0.7.1+ (see README).
headers
is only available inside Publish and Method functions. However, a Method can potentially be called from anywhere, including anonConnection
callback, but this occurs before we've had a chance to reassociate the headers with the DDP connection (see README). Currently, this results in the following incorrect and misleading error being displayed, simply because the header data isn't available yet.Current status: low priority until it's use is justified.
Short term fix: better error message
Long term fix: We could potentially get access to the headers sooner if we send the headers token when opening the DDP connection using cookies, or we could wait for Meteor core to provide a better way.
method headers are still available, and you can get to them like this: