How to close connection in Action cable?
Asked Answered
C

6

13

How to disconnect a client in Action cable (rails 5)? I would like the user to be completely disconnected (similar to when he closes the tab).

Campion answered 8/11, 2016 at 19:56 Comment(0)
C
18

Disconnecting a client from your rails application

If you want to disconnect a client from the rails application, use the disconnect method as described in the documentation: https://api.rubyonrails.org/classes/ActionCable/RemoteConnections.html

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user
    ....
  end
end

ActionCable.server.remote_connections.where(current_user: User.find(1)).disconnect

Disconnecting from the client side

If you want to disconnect the user from the client side you can use the disconnect and unsubscribe functions in your javascript:

App.cable = ActionCable.createConsumer(...)

// Closes the websocket connection.
App.cable.disconnect();

// Unsubscribe from a actioncable subscription (without disconnecting the websocket connection)
App.example = App.cable.subscriptions.create(..);
App.example.unsubscribe();
Chance answered 8/11, 2016 at 20:26 Comment(3)
this drops the subscription or subscriptions - but it doesn't close the websocket.Cesium
@ConfusedVorlon yes, this answer doesn't appear correct.Selfinduced
I updated the answer to have the client close the connection entirely. @TomRossiChance
D
12

I found this inside /var/lib/gems/2.3.0/gems/actioncable-5.0.1/lib/action_cable/remote_connections.rb

If you need to disconnect a given connection, you can go through the RemoteConnections. You can find the connections you're looking for by searching for the identifier declared on the connection. For example:

module ApplicationCable
  class Connection < ActionCable::Connection::Base
    identified_by :current_user
    ....
  end
end

ActionCable.server.remote_connections.where(current_user: User.find(1)).disconnect

This will disconnect all the connections established for
User.find(1), across all servers running on all machines, because it uses the internal channel that all of these servers are subscribed to.

Hope this will be useful. Looks like it works even in Rails console.

Dogwood answered 21/4, 2017 at 10:39 Comment(0)
S
4

Disconnecting from the client

I stumbled across this issue too. But I could not believe that there is no simple way to disconnect the websocket connection from the client (without doing an API call). Luckily this works for me:

// Create consumer
window.cable = ActionCable.createConsumer(...)

// Subscribe to channels
window.cable.subscriptions.create('SomeChannel', ...);

// At some point we want to disconnect (e.g. when user logs out)
window.cable.subscriptions.consumer.disconnect();
Slowpoke answered 13/6, 2018 at 10:13 Comment(2)
when I do this, should I see the websocket under the Network > WS go away? I see in console the connection has ended but I still see the WS in console lingering.Reproach
@NickRes I'm seeing the same thing (four years later). But if I tap on the connection under the WS tab, and then tap on the 'Response' subtab, I see 'Connection Closed'Middleoftheroader
C
2

to disconnect from the client side (in js), call

App.cable.disconnect();

to disconnect from the server side - see the answer from @prograils

Cesium answered 16/1, 2019 at 14:35 Comment(0)
T
1

My work around was to create a new route just for disconnect.

def disconnection
    ActionCable.server.remote_connections.where(connected_user: user_params['email']).disconnect

   render json: {}, status: 200 
end

The client side would have to call the endpoint... something like

PUT  /api/cable/disconnection
Tucky answered 8/4, 2018 at 12:8 Comment(0)
M
1

Closing all websocket connections from a channel,

connection.close

Unsubscribing from client side

App.cable.disconnect();

Disconnecting from outside connection/channel

ActionCable.server.remote_connections.where(current_user: User.find(params[:id])).disconnect
Misfeasance answered 14/12, 2021 at 10:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.