Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between `stream_from` and `stream_for` in ActionCable?

The descriptions here seem to imply that stream_for is used only when passing in a record, but overall the documentation is rather vague. Can anyone explain the differences between stream_from and stream_for and why would you use one over the other?

like image 219
Xavier Avatar asked Aug 17 '16 16:08

Xavier


1 Answers

stream_for is simply a wrapper method of stream_from with ease.

When you need a stream that is related to a specific model, stream_for automatically generates broadcasting from the model and channel for you.

Let's assume you have a chat_room instance of ChatRoom class,

stream_from "chat_rooms:#{chat_room.to_gid_param}"

or

stream_for chat_room # equivalent with stream_from "chat_rooms:Z2lkOi8vVGVzdEFwcC9Qb3N0LzE"

the two lines of code does the same thing.

https://github.com/rails/rails/blob/master/actioncable/lib/action_cable/channel/streams.rb

like image 137
kevinhyunilkim Avatar answered Sep 17 '22 19:09

kevinhyunilkim