Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to close connection in Action cable?

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).

like image 607
Slesarenko Alexander Avatar asked Nov 08 '16 19:11

Slesarenko Alexander


People also ask

How do I cancel Actioncable?

you can unsubscribe by calling the unsubscribe() function on the channel returned by creating a new subscription.

How do you test an action cable?

Testing Action Cable To test this connection class, we can use connect method to simulate a client server connection and then test the state of connection is as expected. The connection object is available in the test.

What does action cable do?

1 What is Action Cable? Action Cable seamlessly integrates WebSockets with the rest of your Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable.


4 Answers

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();
like image 76
siegy22 Avatar answered Nov 15 '22 21:11

siegy22


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.

like image 44
prograils Avatar answered Nov 15 '22 22:11

prograils


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

App.cable.disconnect();

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

like image 36
Confused Vorlon Avatar answered Nov 15 '22 21:11

Confused Vorlon


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();
like image 23
zarathustra Avatar answered Nov 15 '22 21:11

zarathustra