My goal is to have a post feed that will be automatically updated for all users. So, that’s what I try so far:
Added this in my layout:
<%= turbo_refreshes_with method: :morph, scroll: :preserve %>
<%= yield :head %>
Added the brodcasts_refreshes in the model that I want to brodacast updates:
class Post < ApplicationRecord
broadcasts_refreshes
end
Then, in my index, I tried two ways: Broadcasting each post:
# index.html.erb
<div id="posts">
<%= render @posts %>
</div>
# _post.html.erb
<%= turbo_stream_from post %>
<div id="post_<%= post.id %>" class="bg-white shadow rounded-lg p-6 mb-4">
[...]
</div>
Or broadcasting all posts without broadcasting each post:
# index.html.erb
<%= turbo_stream_from @posts %>
<div id="posts">
<%= render %>
</div>
Additional info: I’m using Sqlite with Litecable. In the logs I can see this when I CRUD a post: [ActiveJob] Enqueued Turbo::Streams::BroadcastStreamJob (Job ID: d38e47a7-d593-49bc-8c1d-2a0b8d5fda89) to Async(default) with arguments: "posts", {:content=>"<turbo-stream request-id=\"0b3ad242-1693-4bec-90a7-64856dc10cc0\" action=\"refresh\"></turbo-stream>"}
What is wrong with my approach?