How to access client IP address in Meteor? [duplicate]
Asked Answered
A

4

10

This seems like a very basic question that doesn't have an elegant solution/answer out there.

How can I access the client (remote) IP address from (1) the server or (2) the client?

Anastasius answered 29/8, 2013 at 10:6 Comment(2)
See: #103105Nagging
no idea what cgi-bin is or how to use it for this purpose... :/Anastasius
R
7

As Florin mentioned, this is all pretty much integrated with Meteor now, as opposed to the dark ages when we had to do it ourselves. However, I've additionally wrapped it in a package that tracks all open connections and allows you to query for their IPs: https://github.com/mizzao/meteor-user-status. It also does a bunch of other useful stuff.

Retort answered 5/6, 2014 at 12:30 Comment(0)
W
18

Getting the client IP:

Without a http request, in the functions you should be able to get the clientIP with:

clientIP = this.connection.clientAddress;
//EX: you declare a submitForm function with Meteor.methods and 
//you call it from the client with Meteor.call().
//In submitForm function you will have access to the client address as above

With a http request and using iron-router and its Router.map function:

In the action function of the targeted route use:

clientIp = this.request.connection.remoteAddress;
Wines answered 26/3, 2014 at 9:53 Comment(3)
Salut Florin, how about mentioning the question is a perfect duplicate of this one?Footboard
I totally agree, at that time I wanted to place a comment with that instead of answering but I didn't have enough reputation points (posting a comment at questions/answers that don't belong to you required 50p or more)Wines
This is only available after version 0.7.1.1 for those who have applications running on older versions, like I had.Sleeper
R
7

As Florin mentioned, this is all pretty much integrated with Meteor now, as opposed to the dark ages when we had to do it ourselves. However, I've additionally wrapped it in a package that tracks all open connections and allows you to query for their IPs: https://github.com/mizzao/meteor-user-status. It also does a bunch of other useful stuff.

Retort answered 5/6, 2014 at 12:30 Comment(0)
P
2

On client

headers = {
    list: {},
    get: function(header, callback) {
        return header ? this.list[header] : this.list;
    }
}

Meteor.call('getReqHeaders', function(error, result) {
    if (error) {
        console.log(error);
    }
    else {
        headers.list = result;
    }
});

On server:

headers = {
    list: {},
    get: function(header) {
        return header ? this.list[header] : this.list;
    }
};

var app = typeof WebApp != 'undefined' ? WebApp.connectHandlers : __meteor_bootstrap__.app;
app.use(function(req, res, next) {
    reqHeaders = req.headers;
    return next();
});

Meteor.methods({
    'getReqHeader': function(header) {
        return reqHeaders[header];
    },
    'getReqHeaders': function () {
        return reqHeaders;
    },
});
Putout answered 3/9, 2013 at 8:7 Comment(1)
This is obsolete now. The client IP is now offered via the core clientAddress - docs.meteor.com/#/full/meteor_onconnectionFootboard
K
1

You can use this package: https://github.com/gadicohen/meteor-headers. It gets headers on both client and server.

If you want to do it without a package, you can 'inspire' yourself from the code above, the thing to remember is that prior to 0.6.5 we used the 'hidden' __meteor_bootstrap__.app and post 0.6.5 it's recommended to use WebApp.connectHandler instead.

Kneecap answered 29/8, 2013 at 14:3 Comment(2)
That is a very nice solution :)Anastasius
didn't quite work on the server -- maybe I am doing it wrong: github.com/gadicohen/meteor-headers/issues/2Anastasius

© 2022 - 2024 — McMap. All rights reserved.