Why is handle_info not getting called in my LiveView app?

Since LiveView is a process and the mount/2 callback is the code ran when the LiveView process starts up, you’ll want to subscribe to the same topic where your message {:data, data} is being broadcast.

LiveView runs the mount/2 twice, the second time when the web socket is connected. This means it’s a good idea to subscribe only when connected to the socket. You’ll see a lot of LiveView mount/2 code like this:

def mount(_session, socket) do
  if connected?(socket), do: SomePubSubSystem.subscribe("some_topic")
  get_data(nil)
  {:ok, assign(socket, data: [])}
end
3 Likes