deerob4
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:
-
Use
Registry.lookup/2to get the pid of theLobbyprocess, thensendfrom the countdown timer to be handled inhandle_info. -
Add a
countdown_completed/1function to the lobby that takes the lobby id and callsGenServer.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
RegistryorPhoenix.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!
Trending in Questions
Other Trending Topics
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #library
- #deployment
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #elixirconf
- #channels
- #exunit
- #discussion
- #code-sync
- #podcasts
- #javascript
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #ai
- #elixirconf-us
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #elixirconf-eu
- #metaprogramming
- #hex










Showing Posts 1 to 7- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
LostKobrakai
I’m wondering why you need two processes here in the first place. Couldn’t the lobby do it’s own countdown.
Fl4m3Ph03n1x
Friendly code
I can see you tried to do some homework and you have some options to go about. Before anything, I would recommend you have a look at the examples for “Elixir in Action” (2nd edition):
I would redirect your attention to the Database hierarchy. @sasajuric also uses GenServers and Registry with via tuples, so I believe that just checking 3 or 4 files from his code would already direct you in the right path (right path being what I would recommend xD )
Direct call from CountdownTimer to Lobby
I discourage this. This means that CountdownTimer needs to know about the implementation of Lobby. This is highly discouraged in Elixir, if yo want both processes to communicate, they should have public API’s.
Public API
I believe this is what you mean for your second approach. You have a public function
countdown_completedthat other processes can call. This is by far the preferred method in the community because you expose an API, which allows you to change the structure of the messages being passed without affecting clients. I would caution against using a cast (CountdownTimer needs to make sure that Lobby received the message after all, or the game will never start, right?) but overall this would be my choice.PubSub
Having in mind you will only have 1 process subscribed to the countdown, I believe a PubSub would be overkill. PubSub is nice if several processes are interested in knowing that the CountdownTimer has ended. If you only need to notify 1 process, then you don’t need the added complexity of the PubSub pattern.
peerreynders
This is a valid question. The scope of the problem you have described could be solved with the Lobby process using
Process.send_after/4and simply process the “complete”msgvalue when it is returned via thehandle_info/2callback.Now lets say there is a good reason for the
CountdownTimerprocess to exist - maybe because it somehow coordinates a UI display of the countdown timer.You can take inspiration from
send_after. So rather than definingLobby.countdown_completed/0, you solve the problem in the CountdownTimer API when the countdown is initiated.CountdownTimer.new_timer(completed_msg, time)CountdownTimerwill implement this as acallso it will getfromfor free.send_afteritself with a{from, completed_msg}message value{from, completed_msg}in the process state for laterSo when the time comes
CountdownTimersimply usesGenServer.cast(from, completed_msg)to return the requestedcompleted_msgvalue to the Lobby that requested the timer (which it then has to handle in its ownhandle_cast/2).In this particular situation I think it’s fine as the
CountdownTimerprocess really doesn’t care if the Lobby process has died. Furthermore theCountdownTimercould just slap a monitor onto each of its client processes so that it can cleanup their timers as soon as possible.srowley
I have a simple auction application that implements a timer for bidding and I simply use
Process.send_after/4andhandle_info/2, and store the timer with the rest of the auction state. My front end can make calls to the server as needed to get an updated timer value, and when the timer expireshandle_info/2receives the message, does some things and lets the front end know that the timer expired.That works fine.
Because the auction server also needs to send a bunch of other messages to the front end I ended up implementing a PubSub type of thing later anyway, to which this approach fit in nicely since my application was already generating a message for timer expiration.
Honestly the most trying part of implementing that was thinking through the various values the timer could have (false, a number, nil) and handling them appropriately, because in my case the timer gets started/paused/reset frequently under different conditions.
deerob4
Thanks all for your replies; they’ve been very helpful.
I’m mainly making
CountdownTimerit’s own process because it seems like a separate concern to the lobby, which is mainly dealing with handing out the different roles in the game. I can definitely see the argument for merging the two, but in my head it seems a bit cleaner to separate them out.I like this idea, but would it still work if the
Lobbyprocess is restarted for some reason? I’d expect the pid to be different then, so thefrompid wouldn’t be correct. I’m also not sure how it’s different to the publicLobby.countdown_completed/1function suggested by @Fl4m3Ph03n1x, which could callGenServer.castitself.LostKobrakai
Imho processes should be separated based on their runtime behaviour (error separation and stuff like that) and not on “concerns” like you’d separate classes by. The lobby and the timer are entangled in their runtime behavior so in my opinion there’s no reason to not run them in the same process.
peerreynders
Have a read through the following topic and the related article: To spawn, or not to spawn?
Roughly (i.e. nothing is black and white):
Also possibly:
The
serverargument forcastcan be aname- so require that as an argument fornew_timer.The bigger issue is, how would a restarted
Lobbyprocess re-aquire the previous state (which may have been responsible for the last process crashing in the first place)? If the state is lost then the timer is meaningless.Coupling is significantly reduced.
CountdownTimer.new_timer(completed_msg, time)lets the client choose the exact format of the message to be handled by its ownhandle_cast/2callback. AndCountdownTimerisn’t coupled to the client module - not even at runtime.