Get network address everytime I run mix phx.server

Sometimes I need to share network address of my machine with my colleague which have access to same network(same wifi). and its really useful if he can connect to my machine and perform queries/requests from their machine eg when I run a vue project, it prints:

Compiled successfully in 16911ms                                                                                                                                                          4:55:26 PM


  App running at:
  - Local:   http://localhost:8080/ 
  - Network: http://192.16*.***.**:8080/

and I can share this network address with my colleagues and they can access web app on my machine. I am wondering if I can get this network address in phoenix app. Thanks

In development mode the application binds only to 127.0.0.1 (or ::1 on IPv6 enabled systems) if I recall correctly. This is to avoid some bad player in the network ruining your development effort by attacking your application.

In general it is considered good practice to have development on localhost only.

If you want to open this up, you have to specify the IP to bind to manually. There is nothing in phoenix what would try to autodetect what it could possibly bind to.

And thats a good thing, as a single computer can have many interfaces and each interface could have many IP addresses.

You do not want to expose an in development software on the wrong IP to the wrong network.

PS: You can totally bind to 0.0.0.0/::, but you should be aware of the consequences, and you still need to manually check your systems interfaces for the IP you want to hand to your co-workers.

Thanks @NobbZ. and I understand this as well. But if I get my inet address using ifconfig on ubuntu(I am not sure if this is different on mac or Windows), I can still connect to my server from other machines on the same network so nothing stopping me already. I just want to get that inet address from the system and on server start print it on the screen(development machine) and if I needed will share it with my colleague who is on the same network

maybe System.cmd will help me(and call ifconfig) but it should be consistent on all machines(mac, ubuntu or windows)

This is the default currently. It’s changed on master on github to localhost only however.

1 Like

@LostKobrakai I dont think binding is the problem/issue here. I dont want to change binding. I want to get network address in phoenix/elixir app

Given the phoenix generators were just changed to bind to localhost only (and therefore only to a single interface) I doubt this can make it into phoenix itself. You could however alias phx.server and run a custom mix task after phoenix started doing your own logging.

1 Like

This works hostname -I | awk '{print $1}' in linux. I should be abke to run this via System.cmd I guess. Hopefully its same for each OS

This is how I got it working(credit goes to):

{ip_string, 0} = System.cmd "hostname", ["-I"]
network_address = List.first String.split(ip_string)
2 Likes