Multiple broadcast to liveview process

Erlang/OTP 24 [erts-] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit]

Interactive Elixir (1.14.5) - press Ctrl+C to exit (type h() ENTER for help)

{:phoenix, "~> 1.7.11"},
{:phoenix_live_view, "~> 0.20.3"},

I have a liveview process that subscribes to a channel. There is a button on that LV page which when clicked spawns a genserver (via dynamic supervisor). The genserver then broadcasts a msg back to the LV process.

Initially, when I click on the button the LV process receives single broadcast. If I click on the b utton again, I receive two broadcasts. Then if I click again, I receive 3 broadcasts.

I would love to solve this but I am not sure where to look at. I replicated this in a fresh new phoenix project.

this is the repo with the bug.

go to localhost:4000/users and u will see a buttn with Click me

Try moving your subscribe call up to the mount function:

if connected?(socket) do

The way it is currently it’s creating a new subscription upon each event that is received, which likely is why you see the number of events going up as you click.

1 Like


I thought it was ok for a process to subscribe to a channel multiple times because it shouldnt create more records in the Registry. I could be wrong too.

I tried your approach but that isnt working as well.

Phoenix.PubSub doesn’t use the Registry module but :pg2. Either way, I’d consider it a code smell if I see subscription on each event.