DEV Community

Discussion on: Real-Time Partial Updates in Ruby on Rails using hotwire-rails

Collapse
 
staleo profile image
Stas Syritsyn

Hey Mike, thx for your efforts and a nice lesson!
Got a question tho. Imagine we need to broadcast from the controller action, not from the model with a proc.
Here's how I do it:

respond_to do |format|
format.turbo_stream do
render turbo_stream: turbo_stream.prepend(:tweets, partial: "tweets/tweet",
locals: { tweet: @tweet })
end
format.html { redirect_to tweets_url, notice: 'Tweet was successfully created.' }
format.json { render :show, status: :created, location: @tweet }
end

This approach works pretty much the same, but the new tweets don't appear in another browser tab! New tweet does show up on the author's tab, but on any other tab/browser/device -- it simply doesn't.
Frankly, I didn't really spend much time digging in the docs, did I miss anything maybe?
Best.

Collapse
 
mikerogers0 profile image
Mike Rogers ✈️

turbo_stream is like an AJAX update to the user who made that request. Personally I'm going to stay far away from it & stick with using broadcast_to with my model.