A phoenix presence meta value based on other connected users

I’m working on a realtime video chat application and the feature that I’m trying to build can be thought of as something similar to:

  • Alice comes online and her metadata has a list of user ids of her friends who are also online and are connected to the same socket

  • Bob comes online and must have his own list, but now we must also regenerate Alice’s list to include Bob’s id

  • Charlie comes online and must have his own list, but now we must also regenerate Alice’s and Bob’s lists to include Charlie’s id

  • Alice leaves, so we must now remove her user id from Bob’s and Charlie’s lists

I’m placing these in the Presence under a key called remote_participant_ids. These lists are unique to each user, so I unfortunately can’t do Object.keys(presence) on the client.

I’ve tried overwriting Presence.fetch/2, but that seemed to lose the remote_participant_ids when calling Presence.syncDiff on the client.

I tried manually calling Presence.list() on each action, but I found that the User with the process calling this would receive an updated list, but nobody else would.

After trying various incarnations of this within my Presence module I’ve tried moving it out to the channel and manually regenerating each person’s list on actions between clients, but that’s also seeing inconsistencies between connected clients.

Here is a gist with my code within Presence.fetch/2 - presence.ex · GitHub

What am I doing wrong here? I assumed that fetch/2 is called and updates everyone’s outgoing presence? What’s the right way to update everyone’s Presence in one go like this, or even to use Phoenix to build something like this?

I’d argue that updating metas of pids other than ones own is a antipattern. It sounds like you’re looking for “show friends presences”. It’s old and using outdated apis, but https://medium.com/@alvinlindstam/phoenix-presence-for-social-networks-5fb67143f0ad discusses a way to handle that usecase.

2 Likes

Hmm, assuming you’re using LiveView, you could handle the filtering at the LiveView process level by having each LiveView server side process subscribe directly to Presence broadcasts rather than each LiveView client subscribe via phoenix.js.

Then you could store each user’s friends as a list of ids in the assigns and check/filter against that. This could be useful approach say if you wanted to show all active users, but elevate a user’s active friends into its own separate section at the top.

Using Elixir as a Presence Client | Phoenix.Presence docs