DynamicSupervisor and GenServer usage

I’m learning Phoenix and OTP by making a simple game. I’ve asked somewhat related to this topic but it’s not really yet clear to me the usage of DynamicSupervisor and GenServer.

My app index page is a LiveView page which displays all the games (Standy, Started, Ended). It also has a button which would create another game. Now new game is created, DynamicSupervisor starts a child with a named Genserver using Registry and via_tuple.

When a particle route together with its id as route param is clicked on the home page, I can then easily calls rhat specific GenServer and manipulate states when the game started or ended.

So far what I’ve understood is in order for me to display all I think I need to know the processes under the supervisor (maybe I need supervisors which_children) and access their own state. I dont know how to access the states of the supervisor children.

My analogy regarding GenServer and DynamicSupervisor when applied to my game requirements.

DynamicSupervisor spawns
Game 1 (GenServer)
Game 2 (GenServer)
Game 3 (GenServer)

Please correct if I’m wrong. Sorry for the noob question.
If there are things to clarify, please let me know.

Thank you.

1 Like

Sounds good.

Yep yep.

I would store their name<->pid mappings somewhere else, probably Registry or ETS or so, have them add themselves to it when created and remove themselves when stored (Registry makes this much easier and reliable).

May I know how would I implement this one. I’m not sure how.

I’d say look at the Registry docs, basically just when you create a new child then have that child register itself into the Registry, then you can look it back up from the registry or get a full listing or so. :slight_smile: