Miguel Angel
Miguel Angel

Reputation: 11

Know local ip server from client in node.js

I have a Node.js server, the client is a mobile app. I want to know which would be the best way to know the local ip of the server. I mean...

Server runs in localhost:8080 and the client has to connect to the server like this 192.168.0.107:8080 (pc's local ip).

I want to guest this IP, 192.168.0.107 without writing it in client code because it could change.

Upvotes: 0

Views: 1671

Answers (5)

Asanka
Asanka

Reputation: 618

I got your question as this, Your client have a private & dynamic IP because it is behind some NAT( i.e firewall), mobile app can request some STUN server to get its public IP and Port, then mobile app can send these data to server to identify the client. Similar to peer-to-peer connection establishment. enter image description here

Read more...

Upvotes: 0

arg20
arg20

Reputation: 4991

My guess is that you have a server with a dynamic ip, you want to connect your client to this IP without hardcoding that to the client's code. I'd say have many options:

  • Set up DNS, so that you connect to a known hostname and then translate that into an IP with your DNS server.
  • Implement/use a discovery service using your network's broadcast address. (A server could regularly broadcast information to a broadcast IP, then your client will receive this information and use it to show the user a list of available servers)
  • Let the client have an input box where he can enter the IP address (this is the easiest way, just have a guy input the ip address, maybe save the address for future autocomplete etc)

Upvotes: 2

jfriend00
jfriend00

Reputation: 707148

Depending upon the network topology details of your actual implementation (which aren't included in your question), you have at least three different options:

Server Directly on Public Interent

On a public network (e.g the open internet), DNS is the normal answer to this type of question. You follow the following steps:

  1. Make sure you have a public IP address. As you may or may not know 192.168.0.107 is not a public IP address. It is a local internet address and cannot be routed on the open, public internet.

  2. With a DNS service, create a DNS record that establishes a link between a host name such as "google.com" and your public IP address.

  3. Code the client to connect to your "name", not your IP address.


Server and Mobile App Only on Private Network

If your mobile app runs only on your private network (perhaps via WiFi), then you can configure your own DNS server on your private network that will do essentially the same thing as the steps above, but will work for a private address like 192.168.0.107 on your private network.


Server Lives on Home Network, Want to Access From Internet

If your server lives in your private network (and thus only has a private IP address), but you want to be able to reach it from the public network, then you need to configure some sort of routing or port forwarding at the boundary between your private network and your public network. In a home router situation, this would involve the following steps:

  1. Get the public IP address of your home network connection.
  2. Set up a public DNS entry on the open internet for that IP address.
  3. If your public IP address is not a static IP address (e.g. it could change over time), then you may be able to use one of the "dynamic DNS" services that are built for this type of situation.
  4. At your home router, configure port forwarding so that incoming requests from the internet on the port your web server is running on (usually port 80) or forwarded to the private IP address of your actual node.js server on your home network.

Upvotes: 0

Golo Roden
Golo Roden

Reputation: 150614

IMHO this is not possible in a really reliable way. There are several reasons for this, including:

  • A server may have more than one IP, e.g. multiple network adapaters.
  • A server has an IPv4 and an IPv6 address.

So either you need to narrow down what you want to achieve or you have to live with some kind of uncertainness.

The easiest way then would be to do a DNS lookup, using the hostname, to get the well-known IP addresses, and then select one of them (however you want to do this, that's up to you).

PS: I'm not perfectly sure that I really got your question. If I understood you correctly, you want to find out the IP address of the client on the client, because you need to send it to the server, right?

Upvotes: 1

LucaSpeedStack
LucaSpeedStack

Reputation: 501

If you are looking for a simple answer, there is a npm module that does this for you, it's really small so just install it and use the instructions to get the ip of the client:

https://www.npmjs.com/package/request-ip

Getting Started:

var requestIp = require('request-ip');

// inside middleware handler 
var ipMiddleware = function(req, res, next) {
    var clientIp = requestIp.getClientIp(req); // on localhost > 127.0.0.1 
    next();
};

Upvotes: 0

Related Questions