Is it possible to get all connected sockets on phoenix?

I want to know socket connected user list on phoenix.
I used Presence and it works but it notifies all connection changes to all clients. I don’t want it.
The idea I have now is monitoring sockets with worker, but I want to know any better idea.
Thanks!

Track all the users in Presence under a key different to the channel name. This will prevent users from receiving the diff messages.

4 Likes

cool! thx!

1 Like

I had another method found earlier within this forum and that is to
intercept(["presence_diff"]) inside the channel module. That’s the message phoenix tries to send to provide the presence to connected clients. By intercepting you are able the change the outgoing message (even changing it to a no-op and effectively not sending a message to the clients).
To change the presence_diff message you use the following in the same channel module (where you just added the intercept line from above):

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

Because this function returns a noreply it will not send out any presence information to the connected clients.
I guess both methods work but maybe this method conveys your intention a little better.

1 Like

It surely does convey the intention quite well, but not trying to send diffs in the first place is probably the better implementation than trying to send them just to discard them on their way. I’m not sure how performance intensive a noop intercept is, but it has a warning that customizing messages per user can become a performance issue.

If you track users in presence using e.g. something like an internal:users key I feel like the intend can be communicated in a similarly explicit fashion.

2 Likes