Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add WebPlugin to keep running in background #371

Merged
merged 1 commit into from
May 22, 2024

Conversation

cBournhonesque
Copy link
Owner

Fixes #144

All credit for this PR goes to @Nul-led who came up with the idea and did the entire implementation.
I just copy-pasted his code to put it in a WebPlugin. Thank you very much for this amazing solution!

In wasm, the main thread gets quickly throttled by the browser when it is hidden (e.g. when the user switches tabs).
This means that the app.update() function will not be called, because bevy's scheduler only runs app.update() when
the browser's requestAnimationFrame is called. (and that happens only when the tab is visible)

This is problematic because:

  • we stop sending packets so the server disconnects the client because it doesn't receive keep-alives
  • when the client reconnects, it also disconnects because it hasn't been receiving server packets
  • the internal transport buffers can overflow because they are not being emptied

This solution spawns a WebWorker (a background thread) which is not throttled, and which runs
app.update() at a fixed interval. This way, the client can keep sending and receiving packets,
and updating the local World.

TODO:

  • probably don't need world.clear_trackers()
  • make interval configurable?
  • confirm that the WebWorker doesn't get throttled even after long periods?

@cBournhonesque cBournhonesque added C-Bug Something isn't working A-Transport Related to the transport layer labels May 22, 2024
@cBournhonesque cBournhonesque merged commit b43a63f into main May 22, 2024
2 of 4 checks passed
@simbleau
Copy link
Contributor

This has universally great functionality - Should be a plugin one can easily cargo add webworker_update and add low-frequency updates to their application

if window().unwrap().document().unwrap().hidden() {
// Imitate app.update()
let world = unsafe { world_ptr.write().unwrap().as_mut().unwrap() };
world.run_schedule(Main);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Magnifique

@Nul-led
Copy link
Collaborator

Nul-led commented May 23, 2024

@simbleau thanks :p
ill probably make a crate for it then

@Nul-led
Copy link
Collaborator

Nul-led commented May 23, 2024

re: configurable interval

  1. we use format! to make it configurable at startup
  2. we make an actual protocol for changing the duration (could be achieved by replacing setInterval with setTimeout)

@Nul-led
Copy link
Collaborator

Nul-led commented May 23, 2024

re: trottled web workers
doesnt seem to happen (tested over 30 minutes), didnt confirm on webkit since i dont have a mac (feel free to check if anyone does), confirmed that it works for at least 7 minutes on android chromium based (more than that doesnt seem reasonable anyways)

@Nul-led
Copy link
Collaborator

Nul-led commented May 23, 2024

Another note ill save here:
Lightyear doesnt need to send inputs while in background mode, so maybe worth looking into to save bandwidth?

@simbleau
Copy link
Contributor

@simbleau thanks :p ill probably make a crate for it then

Please let me know when it's done, I'd add this to my bevy_rtc project today.

@Nul-led
Copy link
Collaborator

Nul-led commented May 23, 2024

@simbleau thanks :p ill probably make a crate for it then

Please let me know when it's done, I'd add this to my bevy_rtc project today.

maybe make an impl urself for now then, ill prob wont have time until weekend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-Transport Related to the transport layer C-Bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

WASM examples broken if the user switches tabs
3 participants