JavaScript method to scan and discover active IP addresses on local network

I’m developing a mobile app using Cordova and jQuery Mobile that needs to connect to a local server on the same WiFi network. The problem is that the server doesn’t have a fixed IP address, so it could be anywhere on the network like 10.0.0.15, 10.0.0.22, or 10.0.0.47.

My app needs to find which devices are currently online in the local network so I can check each one to see if it’s running my server. The server provides REST API endpoints that return data my app needs.

What’s the best approach in JavaScript to scan the local network and get back a list of active IP addresses that I can then test one by one? I’ve heard about using ping or making HTTP requests but I’m not sure about the security limitations or the most efficient method.

Has anyone solved this kind of network discovery problem before in a mobile web app?

this is a pain in cordova because of browser security limits. I just brute-forced common ip ranges with super short ajax timeouts (1-2 seconds). not pretty, but works well enough for small networks. also check if there’s a cordova plugin for network discovery - might give you better native access.

When developing a Cordova app that needs to discover local devices, I’ve encountered similar challenges. Due to JavaScript’s security model, directly scanning the network isn’t practical within a web app context. Instead, consider implementing a method where your server uses UDP multicast to announce its IP address. This way, your app only needs to listen for the announcement rather than scan for devices.

Alternatively, a method I’ve successfully used involves sending HTTP requests to the broadcast address of your subnet. While this won’t provide a complete list of all devices, it can effectively identify many that are active without requiring extensive permissions. Keep in mind that you may want to use Cordova plugins for more advanced network functionality, as native access can provide the necessary capabilities.

Had this exact problem a few years ago building a device management app. What worked best was a hybrid approach - server broadcasts its presence using mDNS (Bonjour) while the client uses XMLHttpRequest with short timeouts to probe common gateway patterns. Most home routers stick to predictable ranges like 192.168.1.x or 10.0.0.x, so you can limit your search space. Run these requests in parallel, not sequentially - it’s way faster. Watch out though - some mobile browsers cache failed requests, so add a timestamp parameter to avoid that. Also store the last known IP and check it first before doing a full scan.