Is it possible for Phoenix Presence client to not have access to list of users?

Hey is it possible for phoenix presence client to not have access to the list of users tracked? I just needed the server to have access to a list of users.

I’m using Presence to track activity in a multitenant application and don’t want tenants to have access to information of other tenants.

Also, is handle_diff the way to go if I wanted to trigger something on disconnect on the server?

Thanks!

Hello and welcome,

It’s possible to use Phoenix Tracker directly instead of Presence.

To detect disconnect event on channels, it’s possible to monitor them on join…

I usually have a specific process to catch down event from channels, that keeps track of pid and channel info. That way I can catch disconnect events from clients, and act accordingly…

2 Likes

You can also use MyApp.Presence.track/4 to have presence communicate on a topic named differently than your channel.

3 Likes

Do they ? How can they get access to the list ?

Presence automatically disperses the topic changes down to any connected client. This is an out-of-the-box feature.

I strongly recommend using Tracker here, rather than Presence. Presence is literally Tracker + sending diffs to clients + a few API niceties. You can do everything you need with Tracker, and you won’t be fighting against the purpose that Presence exists.

I try to not advertise my book in forum posts, but there is a chapter (10) dedicated to Presence/Tracker and how they’re different and a concrete example of each. It could be worth looking at if you get stuck. It also explains the difference between the two very clearly.

6 Likes

This is only the case if you use MyApp.Presence.track/3 with the socket as first argument: It defaults to the topic of your current channel. When using MyApp.Presence.track/4 you can choose the topic name and if it doesn’t match your channels topic nothing will be distributed. You can still use all the conveniences the module brings over the plain Tracker.

2 Likes

Awesome, thanks for the quick replies and welcome to the forum!

I’ve just begun my journey with Elixir.

I’ll look in the direction that you folks have suggested.

Thanks again!

Thanks for the welcome!

Does the disconnect event on channels also include things like network timeouts?

I believe the Phoenix.js library has a ‘list’ presences method.
https://hexdocs.pm/phoenix/js/#list

Awesome thanks! Will definitely check the book out. I’m in the phase of ingesting a broad range of Elixir material =)

1 Like

I’d still be careful with this approach. I would ensure that a “*” topic isn’t available on the Channel (or else that topic could be hooked into and sensitive diffs could be received). Another thing to be aware of is that the Presence diffs will still be broadcast around your cluster, even though nothing is using them (wasting resources). The second point may be moot since the amount of resources will generally be low.

2 Likes

Appreciate the pointers in the right direction!

Just a quick followup question about Tracker. Do you know if a diff will be triggered upon websocket timeout, or some type of disconnect that’s not graceful?

Thanks!

All types of disconnects will be handled. If it’s not graceful, you may end up waiting the timeout duration, which I believe is 60s. In practice I’ve found that it is almost always immediate

Tracker uses process links under the hood to achieve this. Any failure will cause the process to die and tracker gets that message, and then removes the process from the tracked set.

1 Like

Awesome, appreciate the info!