khurram rashid
khurram rashid

Reputation: 3

how to create action cable connection in rails

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

    identified_by :current_user

    def connect
      self.current_user = find_verified_user
      logger.add_tags "ActionCable","User #{current_user.id}"
    end

    def find_varified_user
      if current_user = env['wardon'].user
        current_user
      else
        reject_unauthorized_connection
      end
    end
  end
end

Upvotes: 0

Views: 1366

Answers (1)

Muhammad Elbadawy
Muhammad Elbadawy

Reputation: 341

first, you generate a channel

rails g channel {channel_name} {the action you will send to}

this will generate the channel in "app/channels" folder

then mount the channel in routes.rb

  #config/routes.rb
     # add this line
   mount ActionCable.server => '/cable'

then you subscribe to the channel from your client app and use the method you've created in your channel

you can review this tutorial Create Chat Using Action Cable

but ignore the client-side part

Upvotes: 3

Related Questions