Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Actioncable broadcast not working from console in Rails

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)
like image 203
puneet18 Avatar asked Mar 02 '17 11:03

puneet18


1 Answers

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
like image 76
Vaibhav Avatar answered Nov 05 '22 16:11

Vaibhav