#bevy-plugin #bevy #web-worker #web #gamedev #wasm

bevy_web_keepalive

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

7 releases

0.3.2 Jul 9, 2024
0.3.1 May 27, 2024
0.2.0 May 25, 2024
0.1.2 May 25, 2024

#342 in Game dev

Download history 20/week @ 2024-06-02 9/week @ 2024-06-09 3/week @ 2024-06-16 8/week @ 2024-06-23 140/week @ 2024-07-07 50/week @ 2024-07-14 6/week @ 2024-07-21 74/week @ 2024-07-28 58/week @ 2024-08-04 55/week @ 2024-08-11 20/week @ 2024-08-18 38/week @ 2024-08-25 76/week @ 2024-09-01 50/week @ 2024-09-08 64/week @ 2024-09-15

229 downloads per month
Used in 2 crates (via lightyear)

MIT/Apache

16KB
198 lines

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

Dependencies

~19MB
~335K SLoC