Phoenix presence list isn't showing last person that joined as online

I’m tracking online users with :after_join callback and after pushing new state I expect to see list of online users in this callback but map object is empty in this process, on the other hand, while creating new connection i see the previous connection in the list of online users.

What could be wrong? Is this expectation correct?

Hello and welcome,

How does your after join looks like? I have one like this…

  def handle_info(:after_join, socket) do
    user = socket.assigns.user
    {:ok, _} = Presence.track(socket, user.id, %{
      username: user.name,
      online_at: System.system_time(:second)
    })
    push(socket, "presence_state", Presence.list(socket))
    {:noreply, socket}
  end

In the Phoenix example the push state is before the presence track. Thus there is a presence state push (without user), then a presence diff (with user)…

I put the presence_state after presence track because the first presence state will include user and there will be no presence diff push.

I would advise to put some code to help us understand your question, and thus have better answers :slight_smile:

Presence list is correct on front end side. But i need to get presence list after tracking.
For Example:

  def handle_info(:after_join, socket) do
    user = socket.assigns.user
    {:ok, _} = Presence.track(socket, user.id, %{
      username: user.name,
      online_at: System.system_time(:second)
    })
    push(socket, "presence_state", Presence.list(socket))

    IO.inspect(Presence.list(socket))
    # Map is empty

    {:noreply, socket}
  end

Adding this line and I get…

IO.inspect Presence.list(socket)

My logs are OK.

%{
  "1" => %{
    metas: [
      %{online_at: 1566756304, phx_ref: "VWfwpRtjkho=", username: "admin"}
    ]
  },
  "2" => %{
    metas: [%{online_at: 1566755805, phx_ref: "Zgt30Q+VRME=", username: "kiki"}]
  }
}

Would you close all the browser tabs that includes Phoenix socket and stop Phoenix server then check the logs?

Still OK. Please show some of your code…

Forget that send the sample code but problem solved and i expected to see online user after connection of user closed which was wrong thought :smile:

I have this same problem. This comes from the book written by Chris McCord. I tried your suggestion of putting push after Presence.track yet to no avail.

My code is here on Github

Did you solve this? I too am having the same problem. Blank presence list, even though the Logger shows it isn’t blank

OK, scrap that. I wasn’t passing in the correct var to the list function.

This works for me:

Presence.list(socket)

I didn’t solve it. I’ll try your solution. Thanks for posting it.