For example: the local IP (not loopback) will change if the active network interface changed. How to detect it? OR: how to detect the active network interface change event?
Asked
Active
Viewed 1,791 times
1
-
The IP changes? Does it go from IPv4 to IPv6 and back? – Ron Maupin Nov 27 '17 at 05:22
-
Related answer: [How to detect network changes using nodejs](https://stackoverflow.com/questions/26259908/how-to-detect-network-changes-using-node-js). The answer is that it's not built into node.js so you'd have to write platform specific code to do so or find a module that has already done that work for you. – jfriend00 Nov 27 '17 at 05:29
-
@RonMaupin includes IPv4 address changes, for example: when WiFi closed, will auto use wired network. The IP address maybe changed. – acrazing Nov 27 '17 at 05:29
-
1@jfriend00 This method just can detect the network connected or disconnected, and is triggered by run a process to ping other address, it's inefficient and cannot detect the IP address change. – acrazing Nov 27 '17 at 05:31
-
You should edit your question to clarify that you mean IP address, not IP. There are only two current IPs: IPv4 and IPv6. As your question stands, it is saying that the Internet Protocol (IP) changes, not the IP address. – Ron Maupin Nov 27 '17 at 05:31
-
Yeah. It's not a great answer because nothing is built into node.js to do it. You will have to build your own native code or find some module that has already done it. I didn't mark your question as a dup because it wasn't a good answer, but it is relevant to point out to you that such a capability is NOT built into node.js so if you build it yourself, you're probably going to have to write platform specific code and you don't even say what platform you are targeting. – jfriend00 Nov 27 '17 at 05:33
-
@jfriend00 Or any syscall could do this? – acrazing Nov 27 '17 at 05:39
-
As I've mentioned a couple times, anyone who might be able to help you would need to know what platform you are trying to do this for because it likely requires platform-specific code and probably even native code. – jfriend00 Nov 27 '17 at 05:44
-
@jfriend00 Ugh. This is sad. Javascript in the browser has had the ability to detect this to some extent for some time now (at least to tell if the user is offline or not, which can be useful in determine whether it even makes sense to try to poll something.) – Michael Jun 08 '18 at 18:14
-
@Michael - What is the actual problem to be solved? Probably this isn't built-in because it's simply not a common need. There appears to be at least one existing add-on module that does it [ip-monitor](https://www.npmjs.com/package/ip-monitor). There are lots of things a browser does that nodejs does not and vice versa - they are different environments with different common use cases. – jfriend00 Jun 08 '18 at 19:03
1 Answers
0
See here Get local IP address in Node.js
os.networkInterfaces() can retrieve list of addresses and interfaces. Create a timeout to detect the appropriate differences and handle it appropriately when change is detected.

John
- 976
- 1
- 15
- 21