puneet18
puneet18

Reputation: 4427

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)

Upvotes: 19

Views: 7755

Answers (1)

Vaibhav
Vaibhav

Reputation: 926

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

Upvotes: 34

Related Questions