All threads / Realtime browser updates with Cable Ready Discussion

Ask A Question

Notifications

You’re not receiving notifications from this thread.

Realtime browser updates with Cable Ready Discussion

Great episode.

Just as a note to anybody watching this who might be wondering, cable_ready pairs perfectly well with Stimulus Reflex as well. You can simply include the cable_ready call in your *_reflex.rb file and it will cause the reflex change to be broadcast out to every connected client at the same time as the reflex (technically right before, but close enough).

Reply

This is so much easier to implement compared to using a front end framework like React. I wish I knew about these concepts earlier!

Reply

Is there a way to create a channel that only broadcasts to a specific signed in user? For example just send the broadcast to the user who created the card instead of everyone currently connected to the application?

Sure, this can be done in my different ways. You could include a "data-user-id='#{current_user.id}'" in your template and set the selector option in your cable_ready call to be "[data-user-id='#{current_user.id}']".

This would work, but if your use case is to only update to the current user, Stimulus Reflex does that out of the box.

Thanks Michael.

It also looks like I can do this by including the user id in the stream name as well and it will only send the update to any connections that user has.

class UserChannel < ApplicationCable::Channel
def subscribed
stream_for "user_channel_#{current_user.id}"
end
end

cable_ready["user:user_channel_#{user_id}"].inner_html(
selector: "#update_div",
html: html
)
cable_ready.broadcast

Reply

Hi! Is there anyone can help me?
I was trying implement the cable_ready by following this tutorial. But unfortunately, the cable_ready didn't work well. It just broadcast the data, but the client didn't receive that data. I also tried cloning the repo from this tutorial to my local machine, it also didn't work. Idk why, I'm still figuring it out.

facing a similar issue.. its broadcasting the data, but not updating the views. wondering if I'm doing something wrong related to action cable. were you able to fix yours?

had to change my cable.yml file to the below and its working fine.

development:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>

test:
  adapter: async

production:
  adapter: redis
  url: <%= ENV.fetch("REDIS_URL") { "redis://localhost:6379/1" } %>
Reply
Join the discussion

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

Join 35,699+ 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.