简体   繁体   中英

How to Use Action Cable With an API-only Application Rails

我是 Rails 的新手,我被要求使用 Rails 实现一个聊天 API 应用程序,我见过 ActionCable 但已经看到我们必须实现一些前端来连接 WebSocket,有没有其他方法可以制作实时应用程序只是使用邮递员的请求,我不完全知道如何做到这一点,谢谢。

I'm also trying to figure out a solution. However this is what I have discovered so far.

  1. I use Firecamp https://firecamp.io/ which has a WebSocket client to connect to my action cable server which has an endpoint at ws://localhost:4000/cable>
  2. I use devise with devise-jwt for user authentication. See my action cable connection class below
    module ApplicationCable
      class Connection < ActionCable::Connection::Base
      include Warden

      identified_by :current_user

      def connect
        self.current_user = find_verified_user!
      end

      def disconnect
        Rails.logger.info("Disconnected: #{self}")
      end

      protected
      
      def find_verified_user!
        token = request.headers["Authorization"].split(" ").second
        decoder = JWTAuth::UserDecoder.new
        decoder.call(token, :user, nil)
      rescue StandardError => e
        logger.debug(e)
        reject_unauthorized_connection
      end
    end
  end

So far this is what I have gathered. I'm able to create a connection from an authenticated user.

Now working on subscribing the verified user to a Channel. I will update this when I figure it out

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM