When is Phoenix Presence's fetch callback called?

Hi all :slight_smile:

From the docs, “The fetch/2 callback is triggered when using list/1 and serves as a mechanism to fetch presence information a single time, before broadcasting the information to all channel subscribers”.

I added an inspect of Map.keys(entries) in the fetch callback, and it seems to be called twice when a new user is tracked, one time with an empty map and the second time with the full list of users in the presence. The client of this app already keeps the records in memory, so ideally fetch would be called only with the new users that joined. Is it possible to know which ids are new?

Another thing, I’m using two topics to track presence: workspace:{id} and workspace_monitor:{id}. The workspace is the topic that calls Presence.track on join, and workspace_monitor is the topic that actually receives the presence_diff event. When a user joins workspace_monitor, even though I’m not calling Presence.list, fetch is also called with the full list of users in that topic.

In summary, why is fetch called sometimes with an empty map? Is it possible to know which sockets just joined in the fetch function (in order to avoid querying records already in the client)? Any help is welcome :slight_smile:

1 Like