Hotwire Discussion

Where is broadcasts_to explained?

It seems I’ve gotten off the Rails track and I’m not sure I should try to get back on, at least trying to use the newest stuff.

After 15 years of Rails we’ve been through a lot of rapidly changing technology. I feel like my brain is full. Turbo may force me to stay on the side track - mainly because I don’t think I need it!

I’ve never attempted to do a single page app. Yes I’ve used grids/vw sizing to try to squeeze an accounting ledger to display on a phone, but not very effectively. I don’t have use for a chat app/twitter clone, which is about the only examples I could find to see how Turbo works.

I’m very thankful for finding Stimulus. That got me out of jquery (and all the css frameworks that depended on it) hole that I had dug. Some of my sprinkles are larger than I’d like, but I feel more comfortable with validating a fairly complex nested form with Javascript than I would by repeated trips to a model. The stimulus handbook, while short, gave you all you needed. You just had to re-read it a few time to see the light.

Turbo seems to be missing a lot. I didn’t see anywhere in it that it talked about broadcasts_to. But then I’m suborn so I’ve been beating my head against the wall trying to figure out Turbo. I remembered that I threw together a BingoCaller app a few years ago. We were considering having Bingo at a VFW but didn’t have the equipment (caller boards etc). After seeing the cost I said ‘Why not just put a PI on a large screen TV’ and feed it from the Caller app. It worked, but we lost interest. It was also somewhat of a kludge it that the view screen was running a javascript refresh timer.

I made another attempt to see if the viewer page could be streamed. I rebuilt the caller page using Turbo and a little stimulus. It was much cleaner than the jquery/coffescript version. No rails ujs. Then I tried to figure out what the broadcast_to stuff was about.

The view page is just a read-only version of the caller page. You just click on the number to update the game calls and hoped the view page would be streamed. I copied what examples of turbo-stream tags I could find put them in the caller and viewer templates. Things still worked until I put the broadcasts_to line in the Game model.

class Game < ApplicationRecord
  serialize :calls, Array
  # broadcasts_to :game
  # after_update_commit { broadcast_replace_later_to :viewer }
  
  def self.current
    self.find_by(status:'current')
  end
end

Then I get

[ActiveJob] [Turbo::Streams::ActionBroadcastJob] [6de1c7a8-999b-4f36-92af-424fd9cd72ec] Performing Turbo::Streams::ActionBroadcastJob (Job ID: 6de1c7a8-999b-4f36-92af-424fd9cd72ec) from Async(default) enqueued at 2021-04-03T18:39:09Z with arguments: "viewers", {:action=>:replace, :target=>#<GlobalID:0x00007fc77db7b938 @uri=#<URI::GID gid://bingo/Game/2>>, :locals=>{:game=>#<GlobalID:0x00007fc77db7aab0 @uri=#<URI::GID gid://bingo/Game/2>>}, :partial=>"games/game"}
  Rendered callers/_button_bar.html.slim (Duration: 0.5ms | Allocations: 178)
[ActiveJob] [Turbo::Streams::ActionBroadcastJob] [6de1c7a8-999b-4f36-92af-424fd9cd72ec] Error performing Turbo::Streams::ActionBroadcastJob (Job ID: 6de1c7a8-999b-4f36-92af-424fd9cd72ec) from Async(default) in 0.71ms: ActionView::MissingTemplate (Missing partial games/_game with {:locale=>[:en], :formats=>[:html], :variants=>[], :handlers=>[:raw, :erb, :html, :builder, :ruby, .......

I assume its looking for a _game partial, which I don’t have but I don’t know what to set.

It’s been a trip. Lot’s of things I don’t or didn’t know. Didn’t know what docker was and what you had to do to run it on OSX.

If someone can point to some doc’s or another example that is not a chat app, I’ll try again.

It may be I have that you can’t do what I thought you can do. Have a page up (viewer) and if anything changes in the model, it’s refreshed. At least I got rid of Rails,ujs.

In the Rails api docs: ActionCable::Channel::Broadcasting::ClassMethods

In the Actioncable Rails guides:

Thank you. I did find a link in the Turbo handbook, but this may clear up some things,

There is just a lot of thing in different places (model, control, view) that are linked together that makes this seem more complicated than it probably is. Trying to figure it out from a demo app is prone to missing things.