SZJX

SZJX

I need to perform some actions when the user leaves a channel (in most cases where they close the tab voluntarily, but there may also be a connection loss/timeout etc.)

According to posts like Phoenix.Presence run some code when user leaves the channel and https://stackoverflow.com/questions/33934029/how-to-detect-if-a-user-left-a-phoenix-channel-due-to-a-network-disconnect, intercepting the "presence_diff" event from Presence seems to be a foolproof way to go, as it should also cover the cases where the connection terminates abnormally.

Strangely, the presence_diff event seems to only be triggered when I track the user via Presence.track (i.e. when the user joins), but not when the user leaves (e.g. when I close my browser tab under dev environment)

Meanwhile, adding a terminate(reason, socket) callback in my channel correctly catches the leave event.

I wonder what could be wrong in my configuration. Or did I not understand the use of Presence correctly?

Example code:

  def join("participant:" <> participant_id, _payload, socket) do
    if socket.assigns.participant_id == participant_id do
      send(self(), :after_participant_join)
      {:ok, socket}
    else
      {:error, %{reason: "unauthorized"}}
    end
  end
  
  def handle_info(:after_participant_join, socket) do
    experiment_id = socket.assigns.experiment_id

    Presence.track(socket, experiment_id, %{
      # keys to track
    })

    # Broadcast something
    # broadcast(socket, ...)
    
    {:noreply, socket}
  end

  intercept(["presence_diff"])

  def handle_out("presence_diff", payload, socket) do
    # Only gets triggered at Presence.track, but not when the connection is closed.
    IO.puts("presence_diff triggered, payload is #{inspect(payload)}")

    leaves = payload.leaves

    for {experiment_id, meta} <- leaves do
      IO.puts("Leave information: #{meta}")
      
      # Do stuffs
    end

    {:noreply, socket}
  end
  
  # This works, however.
  def terminate(reason, socket) do
    IO.puts("terminated. #{inspect(reason)}")

    # Do stuffs.
  end

Showing Posts 1 to 3

SZJX

SZJX OP

OK I think I know what happened: Each "participant:" <> participant_id topic is, as its name suggests, only subscribed to by one participant. Therefore, when that participant quits, nobody is subscribed to the topic whatsoever. Apparently in this case the whole Presence also ceases to function.

This is not really what I expected, as I need to run some cleanup after a participant exits, but under this mechanism, whenever the last participant that’s out there exits, my callback wouldn’t be triggered at all. It appears that Presence is not suitable for this purpose.

michalmuskala

michalmuskala

The issue is that the presence leave event is triggered when the process dies. But the process itself is subscribed to this information, so you can’t really do anything in that process, because it is already dead. If there was a separate process listening for those events, it should work just fine.

SZJX

SZJX OP

I see. Thanks for the reply. I also found a related old thread at How to create a process subscribed to a channel topic to watch the handle_diff events?. Seems that if one wants really foolproof disconnection detection, one’d still need to have an external monitoring process after all then.

— All posts loaded —

Where Next? Top

Trending in Questions Top

stjefim
Hello! Suppose you are building workflow (order / task / payment) processing system with the following requirements: Each workflow con...
New
jonnycharles
I’m in search of an Elixir library that offers PDF generation capabilities similar to Ruby’s Prawn. While there have been discussions abo...
New
spammy
I’m looking to build a personal workflow to quickly deploy web applications written in elixir/phoenix, for local consumption (ie not on t...
New
dli
Before I dive in myself, did anyone successfully sprinkle Hologram into their existing LiveView app? Looking for hints regarding: Addi...
New
roeland
Kia ora, We have been using elixir-google-api to connect to Google Drive. However, with the updates to Tesla due to CVEs this is now bro...
New
bottlenecked
Hi all, I wanted to ask how the community is dealing with post-release steps. Today we have Ecto migrations, which make sure that the db...
New
rahultumpala
Hello, I have an Elixir backend that implements a custom protocol over TCP. I want to load test the backend and assess the performance o...
New

Other Trending Topics Top

JesseHerrick
Hey, I’m Jesse and I’m the main contributor behind Dexter, a full-featured, lightning-fast Elixir LSP optimized for large codebases. It s...
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
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge &amp; Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
ausimian
Emily is an Elixir library that runs Nx computations on Apple’s MLX. Install it as the default Nx backend and Nx, defn, Axon, Nx.Serving,...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews