I have a very simple Rails 7 app with turbo streams.
I have a page that creates cities and another where the result is seen in real time.
Everything works fine on a PC/Mac.
If I use it on a smartphone, it also works.
But if the smartphone goes to sleep and I turn it on again, the page does not refresh.
cities_controller
# POST /cities or /cities.json
def create
@city = City.new(city_params)
respond_to do |format|
if @city.save
format.html { redirect_to city_url(@city), notice: "City was successfully created." }
format.json { render :show, status: :created, location: @city }
@city.broadcast_render_later_to "cities", partial: "cities/created", locals: { city: @city }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @city.errors, status: :unprocessable_entity }
end
end
end
index.html.erb
<%= turbo_stream_from 'cities' %>
<div id="cities_resultados">
</div>
_created.turbo_stream.erb
<%= turbo_stream.prepend "cities_resultados" do %>
<li><%= city.city %></li>
<% end %>
I have tried adding some logic in connection.rb and the call is made correctly when the smartphone wakes up.
connection.rb
def connect
@city = City.last
puts "*******************************"
puts "Connect"
puts @city.city
puts "*******************************"
@city.broadcast_render_later_to "cities", partial: "cities/created", locals: { city: @city }
end
def disconnect
puts "*******************************"
puts "bye bye"
puts "*******************************"
end
log when smartphone going to sleep and wake up
Finished "/cable/" [WebSocket] for 192.168.5.206 at 2022-04-17 11:12:50 +0200
Turbo::StreamsChannel stopped streaming from cities
*******************************
bye bye
*******************************
Started GET "/cable" for 192.168.5.206 at 2022-04-17 11:10:56 +0200
Cannot render console from 192.168.5.206! Allowed networks: 127.0.0.0/127.255.255.255, ::1
Started GET "/cable/" [WebSocket] for 192.168.5.206 at 2022-04-17 11:10:56 +0200
Successfully upgraded to WebSocket (REQUEST_METHOD: GET, HTTP_CONNECTION: Upgrade, HTTP_UPGRADE: websocket)
(0.2ms) SELECT sqlite_version(*)
↳ app/channels/application_cable/connection.rb:4:in `connect'
City Load (0.7ms) SELECT "cities".* FROM "cities" ORDER BY "cities"."id" DESC LIMIT ? [["LIMIT", 1]]
↳ app/channels/application_cable/connection.rb:4:in `connect'
*******************************
Connect
Polonia
*******************************
Finished "/cable/" [WebSocket] for 192.168.5.206 at 2022-04-17 11:12:50 +0200
Turbo::StreamsChannel stopped streaming from cities
*******************************
bye bye
*******************************
[ActiveJob] Enqueued Turbo::Streams::BroadcastJob (Job ID: 46dc2da7-5062-4018-afe8-4bfbf855d64b) to Async(default) with arguments: "cities", {:partial=>"cities/created", :locals=>{:city=>#<GlobalID:0x00007ff50151fb50 @uri=#<URI::GID gid://magic/City/45>>}}
[ActiveJob] [Turbo::Streams::BroadcastJob] [46dc2da7-5062-4018-afe8-4bfbf855d64b] City Load (0.2ms) SELECT "cities".* FROM "cities" WHERE "cities"."id" = ? LIMIT ? [["id", 45], ["LIMIT", 1]]
[ActiveJob] [Turbo::Streams::BroadcastJob] [46dc2da7-5062-4018-afe8-4bfbf855d64b] Performing Turbo::Streams::BroadcastJob (Job ID: 46dc2da7-5062-4018-afe8-4bfbf855d64b) from Async(default) enqueued at 2022-04-17T09:10:56Z with arguments: "cities", {:partial=>"cities/created", :locals=>{:city=>#<GlobalID:0x00007ff5022bd708 @uri=#<URI::GID gid://magic/City/45>>}}
[ActiveJob] [Turbo::Streams::BroadcastJob] [46dc2da7-5062-4018-afe8-4bfbf855d64b] Rendered cities/_created.turbo_stream.erb (Duration: 0.6ms | Allocations: 58)
[ActiveJob] [Turbo::Streams::BroadcastJob] [46dc2da7-5062-4018-afe8-4bfbf855d64b] [ActionCable] Broadcasting to cities: "<turbo-stream action=\"prepend\" target=\"cities_change\"><template>\n <div data-controller=\"redirect\" data-redirect-url-value=\"Polonia\">\n A ver...\n </div>\n</template></turbo-stream>\n<turbo-stream action=\"prepend\" target=\"cities_resultados\"><template>\n <li>Polonia</li...
[ActiveJob] [Turbo::Streams::BroadcastJob] [46dc2da7-5062-4018-afe8-4bfbf855d64b] Performed Turbo::Streams::BroadcastJob (Job ID: 46dc2da7-5062-4018-afe8-4bfbf855d64b) from Async(default) in 8.77ms
Turbo::StreamsChannel transmitting "<turbo-stream action=\"prepend\" target=\"cities_change\"><template>\n <div data-controller=\"redirect\" data-redirect-url-value=\"Polonia\">\n A ver...\n </div>\n</template></turbo-stream>\n<turbo-stream action=\"prepend\" target=\"cities_resultados\"><template>\n <li>Polonia</li... (via streamed from cities)
Turbo::StreamsChannel is transmitting the subscription confirmation
Turbo::StreamsChannel is streaming from cities
The log says that it works, but the update is not done on the screen.
Any idea?
Sorry for my (Translator) english!!!