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

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
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
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
albydarned
Hello all! I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
dokuzbir
Hello, I am trying to convert my lists to string without losing brackets.For start i have 3 map. They look like these buyer = %{ id: ...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
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
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
Mooodi
Given a string, how can I get access to its character by index? Enum.at("my_string", 2) doesn't work. Or rather, not char, but a substr...
New

Other popular topics Top

Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID&lt;0.412.0&gt; terminating ** (Postgrex.Error) FATAL...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
JakeBecker
TL;DR: I’ve just released an implementation of Microsoft’s IDE-independent Language Server Protocol for Elixir. It adds language support ...
1140 51847 244
New
_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
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
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
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
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
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New

We're in Beta

About us Mission Statement