Phoenix.LiveView.get_connect_info
You're seeing just the function
get_connect_info
, go back to Phoenix.LiveView module for more information.
Accesses the connect info from the socket to use on connected mount.
Connect info are only sent when the client connects to the server and
only remain available during mount. nil
is returned when called in a
disconnected state and a RuntimeError
is raised if called after mount.
Examples
First, when invoking the LiveView socket, you need to declare the
connect_info
you want to receive. Typically, it includes at least
the session but it may include other keys, such as :peer_data
.
See Phoenix.Endpoint.socket/3
:
socket "/live", Phoenix.LiveView.Socket,
websocket: [connect_info: [:peer_data, session: @session_options]]
Those values can now be accessed on the connected mount as
get_connect_info/1
:
def mount(_params, _session, socket) do
if info = get_connect_info(socket) do
{:ok, assign(socket, ip: info.peer_data.address)}
else
{:ok, assign(socket, ip: nil)}
end
end