This is not specific to the IP. assign_new is an optimization to allow reusing of values in conn.assigns when mounting the liveview for the initial static render of the http request. For any subsequent mounts conn.assigns is no longer available, which means you need to be able to compute the same value using the function you have as second parameter of assign_new.
@andreyuhai the conn.remote_ip value is just a tuple. So you can pattern match on it to filter out non-IPv4 address (if that is what you want), i.e., {_, _, _, _} is qualifed as IPv4.
Generally, also watch out that you’re getting the client IP and not the reverse proxy IP if you’re deploying behind one. I think this remote_ip | Hex is all you need, at least you did a few phx || plug versions back.
IPv4 can also embedded in IPv6. I wrote a small function to extract one out of the IPv6 if it is embedded:
def unembed({_,_,_,_} = ip4), do: {:ok, ip4}
def unembed({0,0,0,0,0,65535,ab,cd}) do
a = ab >>> 8
b = ab &&& 255
c = cd >>> 8
d = cd &&& 255
{:ok, {a, b, c, d}}
end
def unembed({_,_,_,_,_,_,_,_} = ip6), do: {:ok, ip6}
def unembed(_), do: {:error, :bad_ip}