Hotwire Discussion

What're the differences between `broadcasts` & `broadcasts_to`?

What’re the differences between broadcasts & broadcasts_to?

For instance, when I use broadcasts in share.rb (The model file), I got:
[ActionCable] Broadcasting to Z2lkOi8vbWluaS1zdG9ja21hcmtldC9TaGFyZS81: …

When I use broadcasts_to → (anythingHere) {‘shares’}, I got:
Broadcasting to shares: …


I do my best to explain the differences and inherent possibilities of stream_for/broadcast_to in the Broadcasting to Resources chapter of the CableReady docs.

Deep down, broadcasting to a resource identifier is the exact same thing as broadcasting to a string identifier. The magic is in the conceptual notion that you’re broadcasting to every client that currently has a given resource in scope. Instead of broadcast_to Post.find(1) you could broadcast at posts:1, but that’s not nearly as elegant.

1 Like