为什么Rails ActionCable在没有挂载的情况下可用?

huangapple go评论76阅读模式
英文:

Why is Rails ActionCable available without mounting

问题

I would like to use action cable in my rails 6 application with the ruby_graphql gem.
我想在我的Rails 6应用程序中使用Action Cable与ruby_graphql gem。

I have noticed that when I simply add
我注意到,当我简单地添加

require "action_cable/engine" to my application.rb
require "action_cable/engine" 到我的 application.rb 文件中

and the required code for Connection < ActionCable::Connection::Base, Channel < ActionCable::Channel::Base etc
以及所需的代码,如 Connection < ActionCable::Connection::Base, Channel < ActionCable::Channel::Base 等等

the websocket becomes available under /cable even though I have NOT added
尽管我没有添加,但WebSocket在/cable下变为可用

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

I don't understand why the /cable route would become available without mounting it in the routes file.
我不明白为什么/cable路由会在没有在路由文件中挂载的情况下变为可用。

I have also noticed that my application's resource consumption (in particular CPU) goes up with the simple require "action_cable/engine".
我还注意到,我的应用程序的资源消耗(特别是CPU)在简单地require "action_cable/engine"后上升。

I would have expected that to only happen once I mounted it.
我本来以为只有在挂载后才会发生这种情况。

Any help and insight into this would be greatly appreciated.
对此的任何帮助和见解将不胜感激。

(I made sure to restart all my servers between changes and check the generated routes to see that /cable was not included - I then tested the websocket using new WebSocket('wss://my-domain.test/cable') both in develop & production which both times successfully upgraded to a websocket with a 101 status code)
(我确保在更改之间重新启动了所有服务器,并检查生成的路由以确保没有包括/cable - 然后我在开发和生产中都使用了new WebSocket('wss://my-domain.test/cable')来测试WebSocket,这两次都成功升级到了具有101状态码的WebSocket。)

英文:

I would like to use action cable in my rails 6 application with the ruby_graphql gem.
I have noticed that when I simply add

require "action_cable/engine" to my application.rb
and the required code for Connection < ActionCable::Connection::Base, Channel < ActionCable::Channel::Base etc

the websocket becomes available under /cable even though I have NOT added
mount ActionCable.server => '/cable'

I don't understand why the /cable route would become available without mounting it in the routes file. I have also noticed that my application's resource consumption (in particular CPU) goes up with the simple `require "action_cable/engine". I would have expected that to only happen once I mounted it

Any help and insight into this would be greatly appreciated

(I made sure to restart all my servers between changes and check the generated routes to see that /cable was not included - I then tested the websocket using new WebSocket('wss://my-domain.test/cable') both in develop & production which both times successfully upgraded to a websocket with a 101 status code)

答案1

得分: 2

请查看您正在require的文件:action_cable/engine

更具体地说,是下面的代码部分:

    initializer "action_cable.routes" do
      config.after_initialize do |app|
        config = app.config
        unless config.action_cable.mount_path.nil?
          app.routes.prepend do
            mount ActionCable.server => config.action_cable.mount_path, internal: true, anchor: true
          end
        end
      end
    end

仅仅require引擎的操作会创建一个初始化程序,将路由添加到您的应用程序路由的顶部。

几行代码后,它还启动了服务器:

    ActiveSupport.on_load(:action_cable) do
        app.reloader.before_class_unload do
            ActionCable.server.restart
        end
    end

这就是为什么路由会在那里,而且看起来action_cable似乎可以直接使用,甚至无需明确启动任何内容。这只是在require "action_cable/engine"内部发生的事情。

英文:

Take a look into the file you are require-ing: action_cable/engine

More specifically the following section of code

    initializer "action_cable.routes" do
      config.after_initialize do |app|
        config = app.config
        unless config.action_cable.mount_path.nil?
          app.routes.prepend do
            mount ActionCable.server => config.action_cable.mount_path, internal: true, anchor: true
          end
        end
      end
    end

Just the action of requiring the engine creates an initializer that adds the route to the top of your application routes.

A few lines later it also starts the server

      ActiveSupport.on_load(:action_cable) do
        app.reloader.before_class_unload do
          ActionCable.server.restart
        end
      end

This is why the route is there and action_cable seems to just work out of the box even without explicitly starting anything. It just happens inside require "action_cable/engine"

huangapple
  • 本文由 发表于 2023年7月18日 14:16:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76709968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定