Count channel subscribers on Phoenix 1.1

Hi!

I’m using Phoenix 1.0 and I count the number of subscribers on a topic by running something like this

  def count_subscribers do
    Enum.each Node.list, fn(node) ->
      Node.spawn(node, ThingCount, :count, [self()])
    end
  end

...

  def count do
    for thing <- Repo.all(Thing), into: %{} do
      {thing.id, length(Phoenix.PubSub.Local.subscribers_with_fastlanes(Thing.PubSub.Local, "thing:#{thing.id}"))}
    end
  end

In Phoenix 1.1 Phoenix.PubSub.Local.subscribers_with_fastlanes/2 was changed to take an additional argument, a shard.

How do I use this new API?
Do I have to call it once per shard? So, instead of iterating over Node.list I should just iterate over the number of shards? If so, how do I find out the number of shards?

I won’t need this when the presence API is released, but until then I need some way to count the active subscribers.

Any help is appreciated.

1 Like

The Phoenix.PubSub.Local module was never a public API and shouldn’t be used to get a subscriber count. The correct solution is to use Phoenix.Presence, which will ship with Phoenix 1.2.

6 Likes

Hi!

Thank you for your answer Chris.

Yes, I understand that what we did previously was quite the hack… But, since Phoenix 1.2 is not available yet, is there any way to get a ballpark figure of active subscribers in 1.1? It doesn’t have to 100% correct at all times. Just something to keep us floating until 1.2 drops :slight_smile:

3 Likes

There’s not APIs for you it so the only solution outside Phoenix.Presence is to roll something yourself

2 Likes

Yeah, thank you for your answers.

Any idea how far away Phoenix 1.2 is?

2 Likes