0

In the documentation, Connection section of Ruby on Rails Guide on Action Cable , the word 'delegate' is used and I don't know what they mean. Here's the code they are referring to in the section:

# 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 current_user = User.find_by(id: cookies.signed[:user_id])
        current_user
      else
        reject_unauthorized_connection
      end
    end
 end
end

Here's the explanation where the explanation is given:

Here identified_by is a connection identifier that can be used to find the specific connection later. Note that anything marked as an identifier will automatically create a delegate by the same name on any channel instances created off the connection.

When they say off the connection, do they mean that the word current_user will refer to the same client in an entirely different connection?

heretoinfinity
  • 1,528
  • 3
  • 14
  • 33

1 Answers1

1

No, as far as I understand it means that all channel instances that are created off this particular connection are identifiable (accessible) by the same current_user object as well. It is possible for the same user to have multiple connections, and in that case the current_user objects in both connections would refer to the same user, but generally the current_user refers to the previously authenticated user of the session.

The ActionCable hierarchy goes like this from top to bottom:

  1. Connection: each User has (at least) one connection (AFAIK one connection per window/tab)
  2. Channel: there can be as many different channels as you want to create (e.g. appearances_channel and web_notifications_channel)
  3. Room: each channel can have multiple rooms. Rooms are like instances of a channel

As for the word 'delegate': it refers to a common Ruby practice called Delegation.

Hope this helped! :)

megahra
  • 295
  • 2
  • 19