Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionCable: close the connection manually from front-end

I can not close my socket manually. It quits only when I close a tab in the browser.

Finished "/cable/" [WebSocket] for 127.0.0.1 at 2016-10-29 16:55:49 +0200

But when I call either

App.cable.subscriptions.remove(App.subscription)

or

App.subscription.unsubscribe()

the method "unsubscribed" in CommunityChannel is being called, but the cable is still present and I can still print it in my function "print_socket"

How to manually close the connection?

module ApplicationCable
  class Connection < ActionCable::Connection::Base

    identified_by :current_user

    def connect
      self.current_user = find_verified_user
    end

    protected

    def find_verified_user
      if current_user = User.find_by(id: cookies.signed[:user_id])
        current_user
      else
        reject_unauthorized_connection
      end
    end

  end
end
like image 285
opla Avatar asked Oct 29 '16 16:10

opla


1 Answers

App.cable.subscriptions.remove(App.subscription) will unsubscribe you from "CommunityChannel", but won't close your connection,

If you want to disconnect, then just do:

App.cable.disconnect()

like image 141
Viktor Avatar answered Oct 22 '22 02:10

Viktor