marat

marat

How to disconnect or update Drab socket or channel on user signout

After I built a chat example using the good tutorial (Drab: Server Side User Interface Access) and this forum (What is the ~E sigil in the Drab tutorial?) I got into this problem. My application UI is built with React and logout is sent using DELETE /sessions request via ajax.

The controller on the backend processes the request to delete session and this is where the chat channel/socket should get disconnected.

Apparently, I can’t find the way how to disconnect the drab socket.
AppWeb.Endpoint.broadcast!("__drab:global", "disconnect", %{})
AppWeb.Endpoint.broadcast!("__drab:same_path:/", "disconnect", %{})
don’t work.

I’m using

  channel "__drab:*", Drab.Channel
  def connect(params, socket) do
    Drab.Socket.verify(socket, params)
  end

in the UserSocket definition, and

  {:ok, socket} = connect(UserSocket, %{})
  socket.endpoint.broadcast!(socket.id, "disconnect", %{})

doesn’t work as well.

Any clues?
Thanks!

Marked As Solved

Also Liked

grych

grych

Creator of Drab

@OvermindDL1 is right, the best way to disconnect is just to kill the channel (or drab - they are linked). You may get the channel pid from %Phoenix.Socket{}, or drab pid from Drab.pid(socket).

But actually, I have no idea how can I kill socket from the Ajax call in the controller - because this was a question. I mean, which socket shall I kill? You might try to pass the Drab socket to the server, but it is like scratching right ear with the left hand…

Can’t you disconnect using Drab handler? Or - easiest way - just render the page with “normal” way in the controller? You are logging out anyway, so refreshing the whole page is quite normal.

And why do you want to disconnect? Maybe we should start with this.

BTW please do not construct topics like "__drab:same_path:/" yourself. This pattern is private and may change, pls use same_path/1 etc helpers instead.

grych

grych

Creator of Drab

Nope, this function is not documented, so private. Please don’t use it :slight_smile:
I meant killing the socket with something like

defhandler disconnect(socket, _) do
  Process.kill(Drab.pid(socket), :normal)
end
marat

marat

Yeah, that’d be awesome!
So far will stick to @OvermindDL1 recommendation:

Last Post!

marat

marat

Yeah, that’d be awesome!
So far will stick to @OvermindDL1 recommendation:

Where Next?

Popular in Questions Top

vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
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
Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
jerry
Good day to you all. I have been struggling to get a query involving like and ilike to work. Can anyone assist me on this, please? pro...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New

Other popular topics Top

rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list. ...
New
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
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
msaraiva
Surface is an experimental library built on top of Phoenix LiveView and its new LiveComponent API that aims to provide a more declarative...
564 44139 214
New

We're in Beta

About us Mission Statement