ActionCable: close the connection manually from front-end
Asked Answered
S

1

7

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
Sunless answered 29/10, 2016 at 16:2 Comment(3)
TMI in the question. I believe you can make it shorter.Chlo
thanks, and sorry for the long post, i was stuck on it all the day:)Sunless
Perhaps this helps: #40495851Philippine
C
8

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

Chlo answered 29/10, 2016 at 19:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.