18

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)
puneet18
  • 4,341
  • 2
  • 21
  • 27
  • I still have the same issue, even with the right redis config, any other suggestions? – rpasianotto Mar 28 '17 at 22:31
  • what are the error messages you can in terminal? – puneet18 Mar 29 '17 at 05:17
  • I fixed the issue too. I was pointing my cable to a different DB id in redis other than the one in the `config/initializers/redis.rb` of the application – rpasianotto Mar 29 '17 at 08:37
  • Possible duplicate of [ActionCable.server.broadcast from the console](https://stackoverflow.com/questions/35176934/actioncable-server-broadcast-from-the-console) – reesaspieces Oct 14 '19 at 12:05

1 Answers1

29

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
Vaibhav
  • 858
  • 10
  • 13
  • 1
    This should be the accepted answer. BTW, I do think SO should allow highest voted answer to be automatically accepted if a certain length of period (say 3 months) elapsed. – lkahtz Aug 23 '22 at 03:53