coop

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

akoutmos

Author of Build a Weather Station with Elixir and Nerves

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

sb8244

Author of Real-Time Phoenix

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

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!

iamMrGaurav

iamMrGaurav

hey, @coop why sometimes user count won’t decrease even after leaving the channel?

Where Next?

Popular in Questions Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
New
hariharasudhan94
Lets say I have map like this fetching from my database %{"_id" =&gt; #BSON.ObjectId&lt;58eb1a7a9ad169198c3dXXXX&gt;, "email" =&gt; ...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement