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

After I built a chat example using the good tutorial ( 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)

in the UserSocket definition, and

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

doesn’t work as well.

Any clues?

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?

1 Like

@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

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.


so yes, if I use just use Drab.Socket without overriding connect function, with
broadcasting :global in Commander and
AppWeb.Endpoint.broadcast("__drab:global", "disconnect", %{}) in controller,
that still doesn’t work.

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:

1 Like

@grych thanks for your reply. Trying to disconnect to not show private messages addressed to logged in user.

I guess this would be the right way. I see the Drab.socket.disconnect() function in js - is that what you mean?

Yes, but drab uses own socket names, starting with __drab:.

Drab’s JS client does not handle anything called “disconnect”, so if you send it to the browsers, it’s ignored. Does Phoenix Socket do it by default?

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(, :normal)

Drab uses it’s own channel names, sockets don’t have names, only that singular ID field that you yourself specifies.

Yes, when sending a message to the socket PID, not a channel. You have to use the id method in the socket module to set the socket identification name.

1 Like



Thanks @OvermindDL1, I’ll give it a try!

I always thought this “disconnect” message there is just an example, and you have to make it yourself! Thanks :slight_smile:

BTW do you guys think we need to add such features like supporting user_ids, disconnections of all users with the same id, new helper same_user/1, etc etc?

1 Like

It is specifically handled, nothing else is as far as I know. :slight_smile:


I end up passing a binary ‘account_id’ all over the place in all my projects (along with a session_id to uniquely identify sessions) via my conn and socket’s.

1 Like

OK, I will think about it. I have no experience with this topic, so if you could suggest an API in the spare time, or just guide me which way to go… :slight_smile:

I need to overhaul it in my ircbot’s web interface sometime, so I should try to come up with what would work nicely there. :slight_smile:

1 Like

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