artvinn

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?

Showing Posts 1 to 10

OvermindDL1

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. :slight_smile:

kokolegorille

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…

  def terminate(reason, socket) do
    log("#{@name} > leave #{inspect(reason)}")

    "game:" <> id = socket.topic
    user = socket_assigned_user(socket.assigns)

    with pid when is_pid(pid) <- Games.get_game(id),
      game_state when not is_nil(game_state) <- Games.get_worker_state(pid)
    do
      if is_player(user.id, game_state), do: Games.leave_game(pid, user)
      
      if Process.alive?(pid) do
        notify_game_info(socket, pid)
      else
        broadcast!(socket, "game_removed", %{uuid: id})
      end
    end

    :ok
  end
artvinn

artvinn OP

:open_mouth: That helped me a lot. I was unaware of terminate callback. I needed to handle when someone leaves the channel and didn’t know I could catch that in terminate. Thank you.

kokolegorille

kokolegorille

Under the hood, a channel is a GenServer :slight_smile:

OvermindDL1

OvermindDL1

Be careful, terminate is a notification callback, you cannot rely on it ‘always’ being called:
https://gist.github.com/mrallen1/806fe5506132260574af33e99dadd499

sb8244

sb8244

Author of Real-Time Phoenix

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

OvermindDL1

Wouldn’t just using a Monitor be so much safer in a supervision tree though?

sb8244

sb8244

Author of Real-Time Phoenix

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

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

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/2 could be problematic when the connection doesn’t close properly, e.g. times out. Guess people should be aware of it when they consider using terminate/2.

— All posts loaded —

Where Next? Top

Trending in Questions Top

RSP87
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
nseaSeb
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
RemyXRenard
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
velrest
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
brecabral
Documentation While reading the Scoped Routes section, I noticed that the documentation currently refers to a problem without explainin...
New
samoloth
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
FlyingNoodle
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 Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
marciok
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
jimsynz
Beam Bots (or just BB for short) is a framework for building fault-tolerant robotics applications in Elixir using familiar OTP patterns. ...
New
Dmk
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
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
webofbits
With AI doing more of the implementation work, I’ve been wondering how much coding I should deliberately keep doing myself. My main conc...
#ai
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews