I’m creating a game system, which includes a lobby and a countdown timer. Once enough players have joined, the countdown timer begins and upon completion tells the lobby to start the game.
Both are implemented as separate
GenServers, registered using a
via tuple in
Registry. I’m a bit unsure of the best way for
CountdownTimer to notify
Lobby that the countdown has completed. I can think of a few different options:
Registry.lookup/2to get the pid of the
sendfrom the countdown timer to be handled in
countdown_completed/1function to the lobby that takes the lobby id and calls
GenServer.cast/2to do whatever it needs.
CountdownTimercan then call this upon completion. I’m not a fan of this because it seems to tie the two together a bit much.
Implement a pubsub mechanism using either
Phoenix.PubSubso that the lobby can subscribe to the countdown completion event. This also has the advantage that my Phoenix channel could subscribe as well. This seems to be the most flexible solution, but I worry that it might be overkill.
I’d appreciate some advice on which method (or an alternative one) to go for. Thank you!