0

I am very confused about ip addresses and headers. So I'm sorry if my question seems simple.

I have gone through the answers listed here: How to get the user IP address in Meteor server?

But I can't figure out how to make them work. For example, I am trying to use this:

Meteor.onConnection(function(conn) {
    console.log(conn.clientAddress);
});

But nowhere can I find where to place it.

If I place it on the client side it says Meteor.onConnection is undefined. If I place it on the server nothing happens. I don't think you run it on meteor.startup.

Is there someone could give me a hand in explaining where to use this code? Specifically if anyone has built a custom tracking system, that would be great. Do you store ips into database? or how to record for tracking purposes?

Any hint will help! thanks

Community
  • 1
  • 1
socialight
  • 487
  • 1
  • 5
  • 18

1 Answers1

2
  1. Place the code anywhere on the server. e.g. in server/main.js. There is no need to call it from within Meteor.startup.
  2. Start the app.
  3. Open your browser to the appropriate address (e.g. http://localhost:3000) - this will cause a web client to connect and run the code from (1).
  4. Look at your shell output (the shell you used to start the app - not the browser console) and you should see your local IP address (127.0.0.1) printed.

This should work in production as well, however if your app is served behind a proxy (e.g. nginx) you will need to add the appropriate commands to pass the user's IP to your app. For nginx, see this post.

David Weldon
  • 63,632
  • 11
  • 148
  • 146