2

The question is fairly self explanatory. I want to auto-detect my server software within a local network from a webpage. I'm able to send and receive broadcasts with node, but for this to work I need to be able to send or receive broadcasts with in-browser javascript, and then connect directly to my server.

Does anyone know how to do this? Is there a library for it, or am I out of luck?

Otto von Bisquick
  • 177
  • 1
  • 2
  • 17
  • [This](http://stackoverflow.com/questions/20194722/can-you-get-a-users-local-lan-ip-address-via-javascript) might help you. – Ben Fortune Apr 08 '15 at 09:00
  • I don't want my own ip address - I want the ip address of the machine that the server is on. To do that, I figure I need to send and receive a broadcast message. – Otto von Bisquick Apr 08 '15 at 09:14
  • You need to know your local IP so you can calculate which subnet you're on, then proceed to scan it for your server. – Ben Fortune Apr 08 '15 at 09:15
  • I can use the broadcast address of 255.255.255.255 which sends it to everything on my subnet. I could also do it your way, but it would be cleaner (and I would prefer) to be able to broadcast. Your way requires that I constantly be scanning a potentially thousand-host subnet until I find my server. – Otto von Bisquick Apr 08 '15 at 09:33
  • JavaScript doesn't have full access to your network, so a broadcast won't work. The only real access to your network from a browser is done via webRTC and all this can do is find out your local IP. – Ben Fortune Apr 08 '15 at 09:36

1 Answers1

1

I would heartily recommend that you take a look at coreos/etcd, hashicorp/consul or some other service discovery solution which exposes an HTTP interface and JSON data about the location of your services.

Since you cannot access the underlying networking devices from the browser (imagine if I could start probing SO's internal network from my external location), arguably, it takes as much time to set up as it would for you to write a proper Node.js application to discover resources on your network and expose these via JSON to your clients, but using proper service discovery solutions means you can take this to any kind of networking configuration your applications may be running in tomorrow under any kind of circumstances they might find themselves in whilst running (fiber optic cables got cut out between two centers, something hard fell down and broke the switch, something monopolized all the network bandwidth, the IP address of the service changes intermittently, etc.).

Filip Dupanović
  • 32,650
  • 13
  • 84
  • 114
  • Thanks for your tips, but after a browse through etcd and google, there doesn't seem to be a service discovery tool that works with pure javascript (i.e. - through html). However, on my search I did find out that Chrome has an api for udp sockets, which is exactly what I was looking for. For now it only works on Chrome, but hey at least it's something. – Otto von Bisquick Apr 08 '15 at 10:29
  • On a second look, the API is only available for packaged apps, so it still doesn't fit my needs. Oh well. – Otto von Bisquick Apr 08 '15 at 10:41
  • `etcd` exposes a HTTP server for services and clients, although these run on a [non-standard HTTP port](https://github.com/coreos/etcd/blob/master/Documentation/configuration.md#-listen-client-urls) by default (i.e. not on port 80). Here is a Node.js application that exposes a Web UI, [interfacing directly](https://github.com/henszey/etcd-browser/blob/master/server.js#L55) with the `etcd` server and piping client requests (for example, if you would need browser access to `etcd` from an external network). – Filip Dupanović Apr 08 '15 at 11:17
  • I'm not sure that's what I need. I need a client with an unknown ip address to find my node server that is on the same subnet that also has an unknown ip address (or vice versa). With this, I'm pretty sure you would still have to know the address of the server beforehand, but I don't. – Otto von Bisquick Apr 08 '15 at 11:29
  • The idea is that your `etcd` server, which is externally accessible from a public IP, registers with the public discovery service via a procured URL, i.e. https://discovery.etcd.io/4dad70723cf5ac6d838b890b4967fd3b. Your service can then register with the `etcd` server and store it's IP address and other necessary details. The client can then read from the `etcd` server and obtain the service's IP address and so the services and clients can discover each other. – Filip Dupanović Apr 08 '15 at 12:00
  • I've added a ref to [consul.io](http://consul.io), another popular service discovery solution that uses HTTP and DNS for discovery. – Filip Dupanović Apr 08 '15 at 12:05