TheMaikXX

TheMaikXX

Hello,
I am trying to implement an auto-disconnect feature for my websocket.

My first idea would is to approach the websocket (module having use Phoenix.Socket) as a regular GenServer and just use Process.send_after/3 inside of connect/3 referring to current socket’s process with self/0 like so:

defmodule MySuperWeb.UserSocket do
  use Phoenix.Socket
  
  @impl true
  def connect(params, socket, connect_info) do
    Process.send_after(self(), :session_timeout, 30_000)
    
    {:ok, socket}
  end
  
  @impl true
  def handle_info(:session_timeout, socket) do
    {:stop, :normal, socket}
  end
end

But this does not seem to do anything and it even gives me warnings that the handle_info/2 already defined in Phoenix.Socket already matches.. so this is not feasible…

Of course I could create another GenServer that would do this kind of thing, that is, if I have access to correct PID with self/0 inside connect/3 (something like ProcessMurderer.murder_self_after/1 :slight_smile:)

Thanks for help or guidance.

Showing Posts 1 to 2

ruslandoga

ruslandoga

:wave:

Maybe something like this would work:

defmodule MySuperWeb.UserSocket do
  use Phoenix.Socket
  
  @impl true
  def connect(_params, socket, _connect_info) do
    #  or :socket_drain
    Process.send_after(self(), %Phoenix.Socket.Broadcast{event: "disconnect"}, :timer.seconds(30))
    {:ok, socket}
  end
end

Context: phoenix/lib/phoenix/socket.ex at e2ec5e09bc6ade8ba60bf9582362dbd911e055f1 · phoenixframework/phoenix · GitHub

TheMaikXX

TheMaikXX OP

Hello,
Thanks. That will probably be it. I will try this as soon as I get to the PC.

PS: I feel bad for not thinking about looking a level deeper.. :slight_smile:

— All posts loaded —

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
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
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
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
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

erlangforums
A new Erlang announcement has been posted: Original announcement:
New
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
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
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 & Solve. They are GUI (Emerge) and State management (S...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews