The hotwire-rails was just released today by Basecamp. It's a collection of JavaScript & Ruby libaries which allows for making some pretty ...
For further actions, you may consider blocking this person and/or reporting abuse
In a manner of speaking I assume that the
turbo_stream_from
subscribes to the PostsApplicationRecord
'sbroadcast_to
?Additionally is
@posts
available to the view because of the broadcast, or are you just not showing your method implementations?Close! The
turbo_stream_from :posts
tells the users Browser to subscribe to:posts
stream (It just adds a snippet of HTML to the page) & in thePost
model we're telling it to broadcast to the:posts
stream.I didn't post up the code controller action where I set that, I'll aim to do a more in depth write up in the future. The broadcast method passes the current model to
app/views/posts/_post.html.erb
as thepost
value.Thanks! That clarifies things a lot!
Ill keep my fingers crossed and an eye out for your detailed write up!
thanks for sharing, just small question If it uses websockets to update the page, does that mean you’re persisting a connection with every user at all times when they’re using the app? Sounds expensive to scale compared to a SPA + JSON APIs.
That's correct. It's a little expensive, but you'll be able to push down changes from the server the moment the stuff changes as opposed to having the user poll for changes from a JSON API.
The key win here is I can get the read-time updates, without having to write any JavaScript & worry about maintaining state.
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.
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 usingbroadcast_to
with my model.Well done m8!
Can Hotwire turbo_stream two different streams from the same object on the same page?
Tried copying the frame code twice in the view but only one updates live.
yes, that's possible.
Is Hotwire able to broadcast to two different model views? I'm having some trouble with that :/
Yeah it can! Post your code up :)
So I've figured out how to broadcast/render to two different model views, but I'm trying to broadcast/render a different partial/design for the view.
Whenever Hotwire broadcast/renders an object, it looks for the original partial, in my case
questions/question
throughid="questions"
.When broadcasting a new object to a view, the object gets broadcasted from
questions/question
and only renders the desired partialquestions/question_live
once the page has been refreshed, or if thequestions/question_live
is already on the page.Example code
<tbody id="questions">
<%= render collection: @questions, partial: "questions/question_live", as: :question %>
</tbody>
Do you know if there is a way to stop Hotwire from rendering the
id="questions"
from partialquestions/question
and render fromquestions/question_live
?Figured it out.
Question model
live_room show view
<%= turbo_stream_from :live_rooms %>
...
code...
...
room show view
<%= turbo_stream_from :rooms %>
...
code...
...