I have noticed that upon socket connection / room join the subscribed user gets an object containing data related to other channel members. How to.prevent that?
P.S. I am using Presence and Phoenix Channels.
I have noticed that upon socket connection / room join the subscribed user gets an object containing data related to other channel members. How to.prevent that?
P.S. I am using Presence and Phoenix Channels.
This is the point of the presence service : share information about all users that are connected to a channel.
To prevent that, do not pass private information to the Presence.track/3
function, or do not use presence at all.
I simply want to identify server-side only who is online / connected to a channel at any given time (identified by unique subscriber id’s).
intercept the outgoing presence push
intercept(["presence_diff"])
def handle_out("presence_diff", _msg, socket) do
{:noreply, socket}
end
I have manged to get the desired result by:
def handle_info(:after_join, socket) do
{:ok, _} = Presence.track(socket, socket.assigns.user_id, %{
online_at: inspect(System.system_time(:second))
})
# commenting out the following line
# push(socket, "presence_state", Presence.list(socket))
{:noreply, socket}
end
I believe that commenting out this line will just disable pushing the original state of presence, but as you are tracking the channel process, you users will still receive the presence diffs.
Imho if you work on a single node you should use a Registry
with duplicate keys. In your :after_join you would call Registry.register(MyRegistry, channel_topic, metadata)
and then you can call Registry.lookup(MyRegistry, channel_topic)
to get the list of {pid, metadata}
that are currently registered.
In fact, this is also needed to eliminate all data, also on changes (on new joins or leaves). Thank you @outlog.