How to prevent sending information about users subscribed to a topic to other presence subscribers?

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.

1 Like

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.

2 Likes

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).

  1. Propagate membership on a second topic with a related name, allow e.g. “cat_pictures”/“cat_pictures:presence” and give admins access to the presence topic and users only access to the presence topic.
    Or
  2. filter presence objects out in your handle_info callback by pattern matching (this might not work if you’re using Phoenix channels).
2 Likes

intercept the outgoing presence push

  intercept(["presence_diff"])

  def handle_out("presence_diff", _msg, socket) do
    {:noreply, socket}
  end

see Phoenix presence use list only for authorized users

1 Like

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.

2 Likes

In fact, this is also needed to eliminate all data, also on changes (on new joins or leaves). Thank you @outlog.

2 Likes