公告 已升级到 Rails 5,用上了 ActionCable

huacnlee · 2016年02月02日 · 最后由 jason_wu 回复于 2018年02月08日 · 20549 次阅读

现在 Ruby China 已经跑在 Rails 5 上面了,并且已经用 ActionCable 代替了 message-bus 来实现实时的消息通知。

关于 Ruby China 升级 Rails 5 的改动,可以参考 https://github.com/ruby-china/ruby-china/pull/555 这个 PR,或许对你升级到 Rails 5 会有帮助。

由于很多 Gem 都还没有发布 Gem 版本,目前 Ruby China 还在直接应用它们 GitHub 的仓库地址。

其他一些改动:

  • 用 Puma 代替 Unicorn 了,原因是 ActionCable 需要多线程的 Rails App Server
  • 话题页面增加新回复提醒,点击可以自动加载,无需在刷新页面了

对 ActionCable 的感受

  • 结构一开始看起来复杂了些,搞明白怎么用,花了半小时;
  • 用起来以后挺好用的,同时它和 Rails Route 结合得很好,不需要额外启动服务;

ActionCable 实现改动看这个 Commit 就明白了:

https://github.com/ruby-china/ruby-china/commit/7193062fa3b8538af504ba470a23442dbdc01896

部署 Nginx 配置

server {
  server_name ruby-china.org;

  location /cable {
    proxy_redirect     off;
    proxy_set_header   Host $host;
    proxy_set_header   X-Forwarded-Host $host;
    proxy_set_header   X-Forwarded-Server $host;
    proxy_set_header   X-Real-IP        $remote_addr;
    proxy_set_header   X-Forwarded-For  $proxy_add_x_forwarded_for;
    proxy_buffering    on;
    proxy_http_version 1.1;
    proxy_set_header   Upgrade $http_upgrade;
    proxy_set_header   Connection "Upgrade";
    proxy_set_header   X-Forwarded-Proto https;
    proxy_pass         http://127.0.0.1:7001;
    gzip off;
  }
}

速度有加快嗎?

#1 楼 @ksec 以 Ruby China 来看,没有明显的变化

赞,公司的项目也上了 action cable

为什么要 点击自动加载 呢?

#5 楼 @kikyous ? 你说新回复?

#7 楼 @kikyous 如果直接加载,有点突兀,所以改成提示了

#8 楼 @huacnlee 感觉直接加载很好啊,新加载的回复可以用稍微不同的颜色标记一下

10 楼 已删除

#9 楼 @kikyous 这里有很多细节,如果用户没有在一直盯着浏览器,直接加载出来是有问题的

个人的项目也升级了。 不过有些 gem 包还不支持。。

早上项目服务器刚升到 5.0.0.beta2

action cable 与 spa 如何结合?google 了,还想没类似的资料!

就是说不要再多开服务了?(sidekiq 之类?)Amazing!

勤劳的小蜜蜂 :plus1:

噫 上次升级还在想为什么不用 puma,这次升级就果断换上了

升得好快

ActionCable 能支持多大并发连接?资源消耗如何?

问下群主,头像不能更改了。能不能帮忙一下

有没有一些性能与稳定性方面的体验呀?

ActionCable 的并发性能怎么样?有评测吗?现在是用 Node.JS 的 Socket.IO 在做实时消息,但感觉有些业务逻辑和 Rails 应用是重复的,如果 ActionCable 能很好地整合资源,并且有不错的性能,就尽快迁移到 Rails5 了。

赞一个👍👍👍

浏览器终端里可以玩起来了

据最近一段时间 Ruby China 上面运作来看,需要将 ActionCable 独立部署在单独的 Puma 进程上面,避免堵塞。ActionCable 官方文档关于 Deployment 部分也是这么说的:

Action Cable is powered by a combination of websockets and threads. All of the connection management is handled internally by utilizing Ruby’s native thread support, which means you can use all your regular Rails models with no problems as long as you haven’t committed any thread-safety sins.

But this also means that Action Cable needs to run in its own server process. So you'll have one set of server processes for your normal web work, and another set of server processes for the Action Cable.

The Action Cable server does not need to be a multi-threaded application server. This is because Action Cable uses the Rack socket hijacking API to take over control of connections from the application server. Action Cable then manages connections internally, in a multithreaded manner, regardless of whether the application server is multi-threaded or not. So Action Cable works with all the popular application servers -- Unicorn, Puma and Passenger.

Action Cable does not work with WEBrick, because WEBrick does not support the Rack socket hijacking API.

https://github.com/rails/rails/tree/master/actioncable#deployment

另外,还有好多细节也会影响性能的,比如前端需要确保用户关闭页面或退出对应功能的时候,一定会 Unsubscribe 关掉连接,不然后端会保持很多 ActionCable Connection 无法释放。

感觉 ActionCable 还有挺多问题的

“用 Puma 代替 Unicorn 了,原因是 ActionCable 需要多线程的 Rails App Server”,这个理由似乎不成立了

connection to 'wss://ruby-china.org/cable' failed: WebSocket is closed before the connection is established. 我这显示

这次升级 Rails 5,改用 Puma 以后内存状态记录

#29 楼 @dannyxu 但支持多线程且免费的 web server 就 puma 一家,passenger 要企业版才有。。。

#33 楼 @ericguo rainbows 也可以呀,模式选 ThreadPool 模式

#34 楼 @killyfreedom 谢谢,我第一次知道,raibows

#32 楼 @huacnlee 服务器 cpu 是几核的呢?

#33 楼 @ericguo "The Action Cable server does not need to be a multi-threaded application server. This is because Action Cable uses the Rack socket hijacking API to take over control of connections from the application server."

@huacnlee,打开浏览器端 firebug, 发现会定时出现 warning: WebSocket connection to 'wss://ruby-china.org/cable' failed: WebSocket is closed before the connection is established. 这是为什么呢?我根据 tutorial 里面的例子做了一个 server,也发现有类似的问题。 附图

#38 楼 @dannyxu 是有这样的问题,现在还没找到原因

#38 楼 @dannyxu #40 楼 @huacnleeconfig.action_cable.allowed_request_origins 有关系吗?

#41 楼 @ashchan 那个我应该配置正确了的。因为一开始是能连上的,跑一会儿就不行了

#42 楼 @huacnlee 我的问题解决了,参考https://github.com/rails/rails/tree/v5.0.0.beta3/actioncablerequest,应该是 origins 的问题

试试在 config/environments/production.rb 里修改配置:

# Action Cable endpoint configuration
  config.action_cable.url = '/cable'
  config.action_cable.allowed_request_origins = [ 'http://ruby-china.org' ]

#45 楼 @huacnlee 好吧。可能我运行的是 actioncable-examples,且是 In app 的方式(nginx + passenger),那样配置后就没有 warning 了

#46 楼 @dannyxu 你的访问量没上去,可能现在还发现不了我的问题

[ActionCable] current connections: 976

一上线就将近 1K 的连接,检查了好多,还是没查到问题原因,依然过一会儿就连不上了。 本地调试过,看起来连接没有泄露,关掉浏览器以后都正常释放了的。

huacnlee 关于 ActionCable 部署的细节经验分享 提及了此话题。 06月17日 17:22
gyorou [问题] 部署的时候碰到的一些问题 提及了此话题。 09月24日 09:58

@huacnlee

你好!你是否有这样的经历,我使用了 Rails5 的 ActionCable,服务器是 Nginx + Passenger,ActionCable 的 adapter 使用 redis,也进行了 namespace 设置,然后会出现服务器挂掉,如果把浏览器关了,也就是 unsubscribe channel 后,服务器有可以使用了。

xinlangzi 回复

我也遇到这个问题,只要浏览器连上 cable,网站就挂起半天打不开。只要把连上 cable 的页面关闭,网站马上就恢复正常了。服务器使用的是 Passenger+Nginx

折腾了下,翻了下论坛,最后决定自己搞 mqtt server 来实现

Terry.Shi cable wss 的问题 提及了此话题。 08月28日 09:42
需要 登录 后方可回复, 如果你还没有账号请 注册新账号