gpreston

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:

  1. The terminate/2 callback of the GenServer which is exiting. This seems to work well, but my understanding is terminate/2 is not always called (for example, running 1 / 0 in a callback did not call terminate/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.
  2. 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.
  3. 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_exit flag and/or Process.monitor, and I’m not sure if using those from a separate process is the best way to do this when terminate/2 and 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

benwilson512

Author of Craft GraphQL APIs in Elixir with Absinthe

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.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
makeitrein
Hey all, just started picking up Elixir last week and am writing a scraper as a learning project. Baby step #1 is extracting the number ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New

Other popular topics Top

Qqwy
Update: How to use the Blogs & Podcasts section You can post links to your blog posts or podcasts either in one of the Official Blog...
3268 119930 1237
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
axelson
This post is a wiki (feel free to hit the edit button near the bottom right of this post to add your own changes!) This post collects co...
239 45766 226
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
fireproofsocks
Forgive me if this is obvious, but how does one delete a database record WITHOUT selecting it first? https://hexdocs.pm/ecto/Ecto.Repo.h...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 42633 214
New
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers' Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New

We're in Beta

About us Mission Statement