Phoenix LiveView Process State

Published May 09, 2020 by Toran Billups

I've been playing around with Phoenix LiveView off and on the last year and recently a good friend of mine started building a production app with it. One of his first questions as a new Elixir developer was simply "how can I get the state of my live view?" so I thought to capture the steps in a short blog post for anyone who might be asking the very same question.

First you need to get the pid of the live view process. I did this from the mount function of my live view as shown below using `IO.inspect`.

    defmodule TodoWeb.PageLive do
      @impl true
      def mount(_params, _session, socket) do
        if connected?(socket), do: self() |> IO.inspect(label: "pid")
        {:ok, socket}
      end
    end
  

Next, spin up the Phoenix server with iex so we can interact with the REPL.

iex -S mix phx.server

Finally, visit the live view route to execute `mount` and get the pid value. We now have everything required to inspect the state of the LiveView process.

:sys.get_state(pid(0,1234,0))

I put together a Todo MVC example that is heavily component focused for anyone who might be Elixir and LiveView curious but coming from React/Vue/Ember/Angular.


Buy Me a Coffee

Twitter / Github / Email