All threads / Working with action cable and remote redis server on development

Ask A Question


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

  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
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?

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 = "#{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!

Join the discussion

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.