Running IO.inspect on a socket without truncating the result?

Hopefully a very simple question… I am troubleshooting and would like to see all the contents of a given socket using IO.inspect(). I know about using limit: :infinity to show everything in maps, lists, etc without truncation but that doesn’t seem to work with sockets. The printing is truncated with ...

Another weird thing is the socket is printed as #Phoenix.LiveView.Socket<> instead of %Phoenix.LiveView.Socket{} like a normal struct. What is a #Name<> data structure and how is it different than a struct? Couldn’t find where in the docs it is mentioned.

IO.inspect(socket, limit: :infinity)

returns this truncated result:

#Phoenix.LiveView.Socket<
   assigns: %{},
   changed: %{
     changeset: true,
     email: true,
     field_errors: true,
     form_error: true,
     ...
   },
   endpoint: MyAppWeb.Endpoint,
   id: "phx-4dWXZ4j7",
   parent_pid: nil,
   view: MyAppWeb.RegistrationLive,
   ...
 >
2 Likes

LiveView derives the Inspect protocol here: https://github.com/phoenixframework/phoenix_live_view/blob/900b6a3400771425f79d62e208f1fdd6b32aefd9/lib/phoenix_live_view/socket.ex#L8

This causes the output to only include the specified fields and outputs in the <> syntax. You can see an example at https://hexdocs.pm/elixir/master/Inspect.html#module-deriving.

You may be able to use the structs: false opt to IO.inspect to get access to it.

https://hexdocs.pm/elixir/Inspect.Opts.html

:structs - when false, structs are not formatted by the inspect protocol, they are instead printed as maps, defaults to true.
18 Likes

Thanks @sb8244, really appreciate it… been working with Elixir a couple years and amazingly have never read about the inner workings of the Inspect protocol and deriving.

1 Like