bevy_web_keepalive

Crates.iobevy_web_keepalive
lib.rsbevy_web_keepalive
version
sourcesrc
created_at2024-05-24 18:41:49.49648
updated_at2024-12-09 11:00:11.69508
descriptionBevy plugins to keep a bevy app running in the browser despite not being visible
homepage
repositoryhttps://github.com/Nul-led/bevy_web_keepalive
max_upload_size
id1251425
Cargo.toml error:TOML parse error at line 19, column 1 | 19 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Nulled (Nul-led)

documentation

README

bevy_web_keepalive

crates.io docs.rs

Library of bevy plugins to keep a bevy app running in the browser despite despite not being visible

WebKeepalivePlugin

The WebKeepalivePlugin plugin creates a web worker that runs the main schedule to keep bevy running in the background (eg. when the user is on another browser tab).

Usage:

// To add the worker, use add_plugins
app.add_plugins(WebKeepalivePlugin::default())

// Configure the worker like this:
app.add_plugins(WebKeepalivePlugin {
    initial_wake_delay: 1000.0, // 1 sec delay
})

// To change the wake_delay at run-time, access the `KeepaliveSettings` resource in a system
fn system_a(mut settings: ResMut<KeepaliveSettings>) {
    settings.wake_delay = 16.667; // 60Hz updates
}

// To terminate the web worker remove the `KeepaliveSettings` resource
fn system_b(world: &mut World) {
    world.remove_resource::<KeepaliveSettings>();
}

Reasoning: bevy_winit runs it's event loop via requestAnimationFrame. This works well for apps that don't need to run if they are in the background. However there are situations where this is unwanted such as multiplayer games that require a constant connection and cannot rely on reconnecting.

VisibilityChangeListenerPlugin

The VisibilityChangeListenerPlugin plugin registers a listener that fires whenever the app's visibility has changed and updates the WindowVisibility resource while also allowing the Main schedule to run a last time after the app is hidden.

Usage:

// To add the listener, use add_plugins
app.add_plugins(VisibilityChangeListenerPlugin::default())

// To actually run the main schedule, configure the plugin like this:
app.add_plugins(VisibilityChangeListenerPlugin { run_main_schedule_on_hide: true })

// To use the `WindowVisibility` resource, access it in a system
fn system_a(window_visibility: Res<WindowVisibility>) {
    if !window_visibility.0 {
        // Do something that you want to do whenever the window is hidden
    }
}

Reasoning: This may be used to notify internal or external services of user inactivity.

Feature Requirements: listener

BackgroundTimerPlugin

The BackgroundTimerPlugin plugin adds a utility resource which contains a timer that keeps track of time spent in the background. This plugin needs to be paired with the WebKeepalivePlugin to function properly (frame delta time is capped at 250ms in bevy_time by default)

Usage:

// To add the listener, use add_plugins, please note that the WebKeepalivePlugin.initial_wake_delay should be < 250.0 so that we can ensure that the frame delta time won't be capped at 250ms
app.add_plugins((WebKeepalivePlugin::default(), BackgroundTimerPlugin))

// To use the `BackgroundTimer` resource, access it in a system
fn system_a(timer: Res<BackgroundTimer>) {
    if !timer.0.elapsed_secs() > 60.0 {
        // Clientside-timeout the user or something similar
    }
}

Reasoning: Can be used to collect analytics about user behavior regarding window visibility or (clientside) timeout a player in multiplayer games after a certain time of inactivity.

Feature Requirements: timer

Commit count: 36

cargo fmt