# Is there a way to broadcast from the controller instead of the model (Rails)

**URL:** <https://discuss.hotwired.dev/t/is-there-a-way-to-broadcast-from-the-controller-instead-of-the-model-rails/2107>\
**Category:** General\
**Created:** [January 25, 2021, 2:15am UTC](https://discuss.hotwired.dev/t/is-there-a-way-to-broadcast-from-the-controller-instead-of-the-model-rails/2107 "2021-01-25T02:15:25Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![BryTai](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/brytai/32/1313_2.png) [@BryTai](https://discuss.hotwired.dev/u/BryTai)\
**Post date:** [January 25, 2021, 2:15am UTC](https://discuss.hotwired.dev/t/is-there-a-way-to-broadcast-from-the-controller-instead-of-the-model-rails/2107/1 "2021-01-25T02:15:25Z")

</div>

I am trying to create a chat app and would like the messages to be styled differently depending on who’s chatroom is being viewed.

In order to do that I need my `_message.html.erb` partial to be able to see what chat room is being viewed so I have a `hidden_field_tag :room_id, room.id` in my new message form.

The problem is that I’m having trouble finding a way to pass the `room_id` as a local variable to my \_message`partial because I can't pass the params to the`message.rb` model.

I’m wondering if there is a way to call the turbo stream broadcast from the controller action so I can pass the the `param[:room_id]`?

---

<div class="post-metadata">

**Author:** ![domchristie](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/domchristie/32/1134_2.png) [@domchristie](https://discuss.hotwired.dev/u/domchristie)\
**Post date:** [January 25, 2021, 3:20am UTC](https://discuss.hotwired.dev/t/is-there-a-way-to-broadcast-from-the-controller-instead-of-the-model-rails/2107/2 "2021-01-25T03:20:23Z")

</div>

Using the [Hotwire demo app](https://github.com/hotwired/hotwire-rails-demo-chat) as an example, you could adapt something like:

```auto
# messages_controller.rb
# def create …
@viewed_room = Room.find(params[:room_id])
Turbo::StreamsChannel.broadcast_append_to @room, target: 'messages', partial: 'messages/message', locals: { message: @message, viewed_room: @viewed_room }

```

(and remove `broadcasts_to…` from the Message model)

---

<div class="post-metadata">

**Author:** ![BryTai](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/brytai/32/1313_2.png) [@BryTai](https://discuss.hotwired.dev/u/BryTai)\
**Post date:** [January 25, 2021, 5:01am UTC](https://discuss.hotwired.dev/t/is-there-a-way-to-broadcast-from-the-controller-instead-of-the-model-rails/2107/3 "2021-01-25T05:01:32Z")

</div>

Thank you, after trying your solution out I realized I can just do

`@message.broadcast_append_to.....`

within my controller action

---

<div class="post-metadata">

**Author:** ![domchristie](https://yyz1.discourse-cdn.com/flex027/user_avatar/discuss.hotwired.dev/domchristie/32/1134_2.png) [@domchristie](https://discuss.hotwired.dev/u/domchristie)\
**Post date:** [January 25, 2021, 5:59pm UTC](https://discuss.hotwired.dev/t/is-there-a-way-to-broadcast-from-the-controller-instead-of-the-model-rails/2107/4 "2021-01-25T17:59:58Z")

</div>

Ah nice! I failed to spot that the [`broadcast_*` methods are both class and instance methods](https://github.com/hotwired/turbo-rails/blob/20070ca30768c5b88e9f317606bf2cd08ed8eb78/app/models/concerns/turbo/broadcastable.rb).
