Ask A Question

Notifications

You’re not receiving notifications from this thread.

Working with action cable and remote redis server on development

Shreyas S asked in Rails

I'm working on a rails app which uses action cable to notify users on the frontend when an event gets triggered.

I'm using a remote redis instance to work with action cable. So this is my dev env setup in cable.yml

development:
  adapter: redis
  url: redis://:<%= ENV.fetch("REDIS_PASS") %>@<%= ENV.fetch("REDIS_HOST") %>:<%= ENV.fetch("REDIS_PORT") %>/1

I'm able to create a websocket connection successfully without any problems.

But when I broadcast from rails console like this
ActionCable.server.broadcast "channel", { data: data }
It returns 0, which means the message isn't getting broadcasted.

In my cable.yml, if I change my url to local redis url
redis://localhost:6379
I am able to broadcast from rails console successfully and it returns 1

I'm not sure why it works with locally but not with remote redis server. How do I find what the problem is and fix this?

Reply

I further inspected redis via redis-cli monitor and found out that ActionCable.server.broadcast publishes messages to local redis even though the remote redis url is mentioned in cable.yml

That is kinda weird. But I got it to work by directly creating a redis instance and publishing it to the channel

$redis = Redis.new(host: "#{ENV.fetch("REDIS_HOST")}", port: "#{ENV.fetch("REDIS_PORT")}", password: "#{ENV.fetch("REDIS_PASS")}")
$redis.publish "channel", { data: data }

Understanding how Action Cable uses Redis under the hood helped a lot. Many thanks to Chris for making videos on Action Cable!

Reply
Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 87,563+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.