I want to stream model updates using Hotwire in Rails. In the documentation they mention creating a stream using a model but I want to create a dynamic stream per user so that changes to models done by a user are only streamed to that user.
I have a VideoCall model which has and belongs to many users via a joining table so the model looks like this and I have a broadcast as such
class VideoCall < ApplicationRecord
has_and_belongs_to_many :users
broadcasts_to ->(video_call) {
video_call.users.map{|u| "video_calls:user_#{u.id}"}
}, inserts_by: :prepend
# more code here
end
On the ERB template where I want to get the updates to the model I have added
<%= turbo_stream_from "video_calls:user_#{current_user.id}" %>
This works if I have one user in a users_video_calls table. But as soon as there are multiple users in a video_call it does not work and I want to broadcast to the stream of all of those users.
Is the approach I am taking correct and how can I achieve broadcasting to multiple users streams dynamically.