chell
chell

Reputation: 7866

Why does ActionCable continually try to upgrade a connection when it can't find a logged in user

Rails 5.2.1

Routes:

    Rails.application.routes.draw do

      mount ActionCable.server => '/cable'
.
.
.

I have the following code in app/channels/application_cable/connection.rb

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

    identified_by :current_user

        def connect
          self.current_user = find_verified_user
        end

        private

        def find_verified_user
          if verified_user = env['warden'].user
            verified_user
          else
            logger.add_tags 'ActionCable', "The user is not found. Connection rejected."  
            reject_unauthorized_connection
          end
        end
      end
    end

I noticed that when I don't have a logged in user the server logs look as follows:

Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:37:05 +0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:05 +0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] An unauthorized connection attempt was rejected
[ActionCable] [The user is not found. Connection rejected.] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:05 +0700
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:05 +0700
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:37:27 +0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:27 +0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] An unauthorized connection attempt was rejected
[ActionCable] [The user is not found. Connection rejected.] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:27 +0700
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:27 +0700
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:37:49 +0700
Started GET "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:49 +0700
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] An unauthorized connection attempt was rejected
[ActionCable] [The user is not found. Connection rejected.] Failed to upgrade to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:49 +0700
[ActionCable] [The user is not found. Connection rejected.] Finished "/cable/" [WebSocket] for 127.0.0.1 at 2018-12-21 14:37:49 +0700
Started GET "/cable" for 127.0.0.1 at 2018-12-21 14:38:11 +0700

Is it correct and the normal behavior of ActionCable to continually attempt to make a connection until if finds a logged in user?

Upvotes: 0

Views: 601

Answers (1)

ekampp
ekampp

Reputation: 1959

AFAIK, the upgrade isn't because of a not logged in user. I believe this is a function of the WS(S) connection attempting a handshake via HTTP(S) and then upgrading itself.

This is a behavior of your web server layer (Puma, Apache, etc.), and not your application layer (Rails, Sinatra, etc.).

Upvotes: 1

Related Questions