【问题标题】:ActionCable.server.broadcast from the console来自控制台的 ActionCable.server.broadcast
【发布时间】:2016-05-12 15:26:11
【问题描述】:

我可以在控制器中使用以下代码,但不能在控制台中使用(两个开发环境)。我正在使用 Rails 5.0.0.beta2。

ActionCable.server.broadcast 'example_channel', message: '<p>Test</p>'

控制台:

>> ActionCable.server.broadcast 'example_channel', message: '<p>Test</p>'
[ActionCable] Broadcasting to example_channel: {:message=>"<p>Test</p>"}
=> []

如何在控制台中使用它?

【问题讨论】:

    标签: ruby-on-rails ruby-on-rails-5 actioncable


    【解决方案1】:

    ActionCable 在开发模式下的默认行为是使用async 适配器,该适配器仅在同一进程中运行。对于进程间广播,您需要切换到redis 适配器。

    要在开发模式下启用redis,您需要编辑config/cable.yml

    redis: &redis
      adapter: redis
      url: redis://localhost:6379/1
    
    production: *redis
    development: *redis
    test: *redis
    

    【讨论】:

    • 我的 Mac 上有 Redis::CannotConnectError: Error connecting to Redis on localhost:6379 (Errno::ECONNREFUSED)。 :(
    • @YakobUbaidi 您是否安装了最新版本的 Redis 并与您的 Rails 服务器同时运行?
    • 尝试brew install redis,然后:redis-server /usr/local/etc/redis.conf
    • 我仍然收到此错误 NoMethodError: undefined method `fetch' for nil:NilClass
    • 感谢您的回答!您还应该确保在您的 Gemfile 中包含 gem 'redis'。
    猜你喜欢
    • 2020-02-23
    • 2012-10-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多