I have a function to loop through each presence and update the same key. It works on the first run but then returns {:error, :nopresence} on each subsequent one.
Been banging my head on this one. If anyone has any ideas I would greatly appreciate it!
def update_presence_for_each(key) do
for presence <- Presence.list_presences(@topic) do
Presence.update_presence(self(), @topic, presence.user_id, key)
end
end
That function calls the below:
def update_presence(pid, topic, key, payload) do
metas =
Presence.get_by_key(topic, key)[:metas]
|> List.first()
|> Map.merge(payload)
Presence.update(pid, topic, key, metas)
end
def list_presences(topic) do
Presence.list(topic)
|> Enum.map(fn {_user_id, data} ->
data[:metas]
|> List.first()
end)
end
All the right values are being passed each time in the loop, so I’m not sure what it could be.
When I inspect the metas before the update that succeeds, notice it has both phx_ref and phx_ref_prev metas. But on the one that fails, it doesn’t have the phx_ref_prev meta.
I’ve confirmed this pattern happens for every update that fails, even if that update runs first in the loop.
I don’t have the right answer yet but am getting closer and will update you all when I figure it out. I’m assuming there’s an issue with my update_presence function that compiles the metas.