TheMaikXX

TheMaikXX

Send message to `Phoenix.Socket` process (not to the client, but elixir process)

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.

Marked As Solved

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

Last Post!

TheMaikXX

TheMaikXX

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:

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
ovidiubadita
Hey all, I discovered Elixir and I love it. I always wanted to learn a functional programming and I intended to go for Haskell, but afte...
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
New
lanycrost
Hi everyone! I need implement if…else if…else condition from my elixir code, and anymore of this control flow structures not work proper...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
New
gausby
I asked this very same question on twitter and got some interesting feedback, but I thought it would be a good question to ask here as we...
1207 40165 209
New

We're in Beta

About us Mission Statement