I used actioncable on rails 5 app. Below code work in controller but not in console.
ActionCable.server.broadcast "connector_app", content: "test message"
Response:
[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 properly):
def sample_socket_message
ActionCable.server.broadcast "connector_app",
content: "test message",
head :ok
end
Problem Solved: Forget to add below code in config/initializer/redis.rb
$redis = Redis.new(:host => 'localhost', :port => 6379)
The default behavior for ActionCable in development and test mode is to use the async adapter, which operates within the same process only. For inter-process broadcasting, you will need to switch to the redis adapter.
Since you are using redis adapter in development that's why it is working in controller and not working in console.
cable.yml
test:
adapter: redis
url: redis://localhost:6379/1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With