coop
Phoenix Channels: Track the number of connected users per topic per host
I am wanting to build a dashboard that shows the number of users connected to each topic per node so I can visualise the distribution of users across servers.
I was hoping this information was already exposed somewhere in Phoenix but for the life of me I couldn’t figure it out so I wrote a small library to do it - GitHub - coop/phoenix_channel_topic_counter: Expose the number of users connected to a topic per host · GitHub.
Earlier in the year I queried slack but no one had a good solution and I received a few DMs asking for my solution.
If there is a different / better solution available I’d love to hear about it.
Thanks.
Most Liked
akoutmos
I am actually working on some similar stuff in the PromEx Project GitHub - akoutmos/prom_ex: An Elixir Prometheus metrics collection library built on top of Telemetry with accompanying Grafana dashboards · GitHub
At the moment I am using the [:phoenix, :channel_joined] telemetry event to capture metrics on how many channel joins have occurred but that won’t tell you how many people are currently attached to a channel as I don’t keep track of when people leave the channel. As for the channel that the joined event is in regards to, that is in the event metadata under the Phoenix.Socket struct:
event_metadata = %{
params: %{},
result: :ok,
socket: %Phoenix.Socket{
assigns: %{},
channel: Phoenix.LiveReloader.Channel,
channel_pid: #PID<0.633.0>,
endpoint: WebAppWeb.Endpoint,
handler: Phoenix.LiveReloader.Socket,
id: nil,
join_ref: "6",
joined: false,
private: %{log_handle_in: :debug, log_join: :info},
pubsub_server: WebApp.PubSub,
ref: nil,
serializer: Phoenix.Socket.V2.JSONSerializer,
topic: "phoenix:live_reload",
transport: :websocket,
transport_pid: #PID<0.630.0>
}
}
Hope that helps!
sb8244
Someone can correct me here, but I imagine the issue with tracking leaves is that the disconnection event can’t be guaranteed depending on how the process is killed. That may be wrong depending on supervision setup, but my immediate thought goes to some edge cases that prevent complete guarantees.
coop
I assume you’re correct if you are wanting to track disconnects from within the process that is “dying”. That’s why I am monitoring the channel process separately.
Last Post!
Popular in Questions
Other popular topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #elixirconf-us
- #advent-of-code
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #hex









