Gripe:
I thought it was supposed to be “simple” to have a “turbo stream” that automagically populated something in the dom over a socket. Either I’ve completely misconcepualized how this is supposed to work, or… turbo sucks?
I have a this:
<%= turbo_stream_from "connections_notifications" %>
In the controller for that endpoint I have this thing:
PrepSidenavJob.perform_later(current_user)
And that job does this:
Turbo::StreamsChannel.broadcast_replace_to(
"connections_notifications",
target: "connections_notification",
partial: 'product/sidenav_badge',
locals: {count: connections_notification},
layout: nil
This apparently creates a race condition because the order of event are:
- render first template
- drop job
- job processes
- job broadcasts
- client connects to /cable
- client receives nothing
Basically, the job is so fast to broadcast that the client can’t even connect to the websocket. So the results of the job have been broadcast before the client can do anything with it.
This is all largely verifiable by slapping a sleep 3
in the job. But that is not a solution.
So… why isn’t this “easy” ? Am I completely misunderstanding the rails/turbo/magic or is it all marketing?
FWIW mostly recent versions of things:
rails 7.2
@hotwired/turbo-rails 8.0.12