Actioncable broadcast does not work from console in Rails

I used actioncable in rails 5 app. Below code works in the controller, but not in the console.

ActionCable.server.broadcast "connector_app", content: "test message"

Answer:

[ActionCable] Broadcasting to connector_app: {:content=>"test message"}
=> nil

cable.yml

development:
  adapter: redis
  url: redis://localhost:6379/1

test:
  adapter: async

production:
  adapter: redis
  url: redis://localhost:6379/1

Contoller code (it works correctly):

def sample_socket_message
    ActionCable.server.broadcast "connector_app",
      content:  "test message",
    head :ok
end

Solution: Forget adding below code in config / initializer / redis.rb

$redis = Redis.new(:host => 'localhost', :port => 6379)
+4
source share
1 answer

The default behavior of ActionCable in design and testing mode is to use an asynchronous adapter that only works in the same process. For interprocess broadcasting, you will need to switch to the redis adapter.

Since you are using the redis adapter in development, why does it work in the controller and does not work in the console.

cable.yml

test:
  adapter: redis
  url: redis://localhost:6379/1
0
source

Source: https://habr.com/ru/post/1671242/


All Articles