Cannot get handle_info to work in my liveview


I have a weird issue, I am trying to follow the guide for the LiveView to learn about it but I have an issue with a very simple usecase. I would like to have a counter incremented by my backend.

This is my LiveView:

defmodule FooWeb.BarLive do
  use FooWeb, :live_view

  def render(assigns) do
    <div>Test <%= @counter %></div>

  def mount(_params, _session, socket) do
    if connected?(socket), do: Process.send_after(self(), :update, 1000)
    {:ok, assign(socket, :counter, 0)}

  def handle_info(:update, socket) do
    Process.send_after(self(), :update, 1000)
    {:noreply, assign(socket, :counter, socket.assigns.counter + 1)}

I would expect to see my counter going up every seconds, instead in stays at 0 and I get this error after the first second:

[debug] warning: undefined handle_info in FooWeb.BarLive. Unhandled message: :update

I am using LiveView 0.18.11 (from the lock file).

I am pretty sure I am holding it wrong, any idea?

It should work, have you tried recompiling manually, sometimes the automatic recompilation bugs out.

I actually stopped the server and restarted, that should do the trick right?

Yes, either do that, or start the server with the iex by using iex -S mix phx.server, and then executing recompile in the console.

So restarting the server in itself didn’t do the trick but the recompile did :blush: Thanks a lot!