PhoenixKit.Utils.IpAddress (phoenix_kit v1.5.2)
View SourceUtilities for extracting and formatting IP addresses.
Supports both IPv4 and IPv6 addresses with proper error handling. Prevents Protocol.UndefinedError when working with IPv6 tuples.
Usage
Extract from LiveView socket:
ip = PhoenixKit.Utils.IpAddress.extract_from_socket(socket)Extract from Plug.Conn:
ip = PhoenixKit.Utils.IpAddress.extract_from_conn(conn)Extract from peer_data directly:
ip = PhoenixKit.Utils.IpAddress.extract_ip_address(%{address: {192, 168, 1, 1}})Examples
iex> PhoenixKit.Utils.IpAddress.extract_ip_address(%{address: {192, 168, 1, 1}})
"192.168.1.1"
iex> PhoenixKit.Utils.IpAddress.extract_ip_address(%{address: {8193, 3512, 1, 0, 0, 0, 0, 1}})
"8193:3512:1:0:0:0:0:1"
iex> PhoenixKit.Utils.IpAddress.extract_ip_address(nil)
"unknown"
iex> PhoenixKit.Utils.IpAddress.extract_ip_address(%{})
"unknown"
Summary
Functions
Extracts IP address from Plug.Conn using get_peer_data.
Extracts IP address from Phoenix.LiveView socket using get_connect_info.
Extracts IP address from peer_data map.
Functions
Extracts IP address from Plug.Conn using get_peer_data.
Convenience function that extracts peer_data and formats the IP.
Parameters
conn: Plug.Conn struct
Returns
- IP address string or "unknown"
Extracts IP address from Phoenix.LiveView socket using get_connect_info.
Convenience function that extracts peer_data from socket and formats the IP.
Parameters
socket: Phoenix.LiveView.Socket struct
Returns
- IP address string or "unknown"
Extracts IP address from peer_data map.
Handles both IPv4 (4-tuple) and IPv6 (8-tuple) addresses. Returns "unknown" for nil, invalid, or missing data.
Parameters
peer_data: Map with:addresskey containing IP tuple, or nil
Returns
- IPv4 as "a.b.c.d" string
- IPv6 as "a:b:c:d:e:f:g:h" string
- "unknown" for invalid or missing data