gpreston
Broadcasting to channel on GenServer exit
Hi all,
I’m creating an application where at any given point there are a number of active “rooms”. These rooms must have state, so when someone creates a room the application spins up a GenServer to associate with it via a DynamicSupervisor, and it is tracked in a Registry. Rooms can be disbanded a few ways, notably manually by a host, by inactivity, or by error (this last one is where my confusions come in).
When a room terminates, any client still in that room must be notified, so I have to broadcast to the channel topic. This is easy to do with Endpoint.broadcast!, but I’m wondering where this call should be made. I’ve come up with a few options:
- The
terminate/2callback of the GenServer which is exiting. This seems to work well, but my understanding isterminate/2is not always called (for example, running1 / 0in a callback did not callterminate/2). It feels weird not wanting to do something because a runtime exception might cause strange behavior (lol), but also this is Elixir, if there’s a runtime exception I want people to be gracefully kicked out of the room and send back to the lobby so they can create a new room if they like. It also feels a little weird having channel event logic in the GenServer, but not sure if that’s actually something I should be worried about. - The supervisor monitoring/trapping exits. I’ve actually had trouble getting this to work, but if this would be better in theory I would figure it out.
- A different process being in charge of monitoring room GenServers and broadcasting messages on their exit. I kinda like this because it separates concerns of the room servers and room supervisor a bit, but not sure if there’s actually a reason to do this. Plus it would rely on the
:trap_exitflag and/orProcess.monitor, and I’m not sure if using those from a separate process is the best way to do this whenterminate/2and all children are supervised by an existing process already.
Any thoughts and/or resources I should read? I feel like the problem of associating a GenServer with an active topic can’t be a new one but I haven’t come across any resources that describe good patterns for this in-depth. Thank you!
Most Liked
benwilson512
Instead of broadcasting, each client could instead Process.monitor the GenServer for the room. Then it will get a guaranteed :DOWN message from the VM instead of relying on the terminating GenServer to broadcast.







