marat

marat

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

After I built a chat example using the good tutorial (https://tg.pl/drab) 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:

OvermindDL1

OvermindDL1

I kill the entire socket via Phoenix Channel normal disconnect methods.

Drab will then try to reconnect, but if you override the connection event to test a token of your own (that you pass in, it’s documented somewhere) then you can verify that they are not logged in and disallow it, then done. :slight_smile:

/me would normally supply code but is super busy… Maybe @grych can?

OvermindDL1

OvermindDL1

Broadcasting to __drab:global sounds… not right… I use Endpoint.broadcast("user_socket:" <> user_id, "disconnect", %{}) since I id the sockets to kill the socket itself, not a channel. That’s all normal documented Phoenix Sockets stuff. :slight_smile:

Where Next?

Popular in Questions Top

polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
sacepums
Hey guys. I'm new to elixir and im really stocked about it. But I ran into a bit of problem - I need to convert a date sting, for examp...
New
myronmarston
The Elixir Typespec docs show the following syntax for keyword lists in typespecs: # ... | [key: type] # keyword lis...
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
stefanluptak
Hello everybody, usually, I use a 29" ultra-wide monitor for VSCode which can easily accomodate explorer (files panel) + file with code ...
New
LegitStack
I’m trying to make a websocket server in Phoenix or raw Elixir. I heard about gun, I think I could use cowboy, but since I’m not that sma...
New
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
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
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
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

Other popular topics Top

Brian
What is the proper way to load a module from a file in to IEX? In the python world, doing something like this pretty standard: from ....
New
sorentwo
Hello! tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability. After spen...
977 41022 311
New
yawaramin
In the Dialyzer docs ( http://erlang.org/doc/man/dialyzer.html#requesting-or-suppressing-warnings-in-source-files ), there is a way to tu...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: https://hexdocs.pm/ecto/Ecto.Schema.html#module-...
New
grych
Hi folks, Few months ago I have announced the proof-of-concept of the library to manipulate the browsers DOM objects directly from Elixi...
639 49522 488
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
fireproofsocks
I’m working on defining a simple Ecto schema for a table (in PostGres), but I don’t see where I can define a column as NOT NULL. Conside...
New
chrismccord
As promised, the first release candidate of Phoenix 1.3.0 is out! This release focuses on code generators with improved project structure...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
AstonJ
by Lance Halvorsen Elixir and Phoenix are generating tremendous excitement as an unbeatable platform for building modern web application...
460 27162 124
New

We're in Beta

About us Mission Statement