artvinn
Hi. I am making a small 2 players game with Phoenix channels and I am using Presence to track connected users. My use case requires to terminate the game GenServer when any player leaves the channel, and notify another user about it.
So far I’ve achieved this by intercepting the “presence_diff” event in handle_out:
intercept ["presence_diff"]
def handle_out("presence_diff", %{leaves: leaves} = msg, socket) do
# check if someone left
case Enum.count(leaves) > 0 do
true ->
# kill gen server
Engine.kill_game(socket.assigns.game_id)
# notify players via terminate event
push(socket, "terminate", %{})
{:noreply, socket}
_ ->
push(socket, "presence_diff", msg)
{:noreply, socket}
end
end
And then on the client side I would do:
channel.on("terminate", resp => {
channel.leave()
});
So I am wondering if it’s a good way to intercept the “presence_diff” event, do some job there and send an absolutely different event? Or is there any different approach to that?
Trending in Questions
I’m working on a project that simulates the bumbl example in the programming phoenix book. It acts almost like an email client. We have a...
New
Hello,
I know there is an approach for handling lists that allows for optimized traversal, but I can’t recall the specific method (somet...
New
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
So my question is quite simple and i have found no conclusive answer on forum, google or AI.
Should we use :erlang.float for Integer to ...
New
Documentation
While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New
Other Trending Topics
I am happy to introduce the very α version of the new programming language compiled to BEAM.
Welcome Cure.
It has literally three kille...
New
Hi there! We created Gust: A task orchestrator inspired by Airflow.
For those who have never heard about Aiflow, it’s a Python-based wor...
New
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Xamal is a deployment tool for Elixir apps that deploys native releases to bare metal servers over SSH. It’s a port of GitHub - basecamp/...
New
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself.
My main conc...
New
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
- #elixirconf-us
- #ai
- #blog-post
- #elixir-ls
- #phoenix_html
- #iex
- #graphql
- #genstage
- #websockets
- #supervisor
- #advent-of-code
- #distillery
- #processes
- #api
- #forms
- #hex
- #security
- #metaprogramming











Showing Posts 1 to 10- Show Best Posts
- Show All (oldest first)
- Show All (newest first)
OvermindDL1
You can always just register a channel PID to a process that monitors it, then it can do whatever it wants when the monitor tells it the channel PID dies.
kokolegorille
I do have a game channel for each individual game. Then I use join and terminate to signal join/leave of the game channel. I also store players in the game struct to check if You You are allowed to join/leave the channel.
This is an example of my terminate fuction, in the game channel…
artvinn
kokolegorille
Under the hood, a channel is a GenServer
OvermindDL1
Be careful,
terminateis a notification callback, you cannot rely on it ‘always’ being called:https://gist.github.com/mrallen1/806fe5506132260574af33e99dadd499
sb8244
In addition to channel GenServer callbacks, it’s possible to use a technique like Phoenix.Presence uses to know when a channel dies. I extracted a gist (haven’t run it as is, but should work) from a project I’m working on recently.
OvermindDL1
Wouldn’t just using a Monitor be so much safer in a supervision tree though?
sb8244
True, it depends on what you’re wanting to optimize. Presence (and my code example) prefer consistency in the case of a failure. If you lose your monitors because you died, you would have to have a way to re-fetch that information in order to become consistent again. If you link, you will kill the other processes and they will re-connect (in the case of sockets).
Ultimately, it’s a tradeoff of if you believe your process will die and what you want the consequence of that death to be.
OvermindDL1
Yep, if you are just wanting a registry of connected processes then a monitor, but if you want to propogate death then a link, however trapping exits almost certainly means you want a monitor instead, or a supervisor.
SZJX
Indeed, as detailed in this question https://stackoverflow.com/questions/33934029/how-to-detect-if-a-user-left-a-phoenix-channel-due-to-a-network-disconnect, relying on
terminate/2could be problematic when the connection doesn’t close properly, e.g. times out. Guess people should be aware of it when they consider usingterminate/2.