[whatwg] Proposal: requestBackgroundProcessing()
ashley at scirra.com
Thu Feb 20 07:25:56 PST 2014
We're building a browser-based P2P multiplayer engine on top of WebRTC
DataChannels. You can try out our work-in-progress here:
The first player in to a room is assigned the host, and all other
subsequently joining peers connect to the host with WebRTC DataChannels and
start exchanging game data. Like all responsible HTML5 games, it runs based
on a requestAnimationFrame loop. When switching tab, the
requestAnimationFrame loop stops, and most browsers also limit timers to 1
Hz in background tabs. This is a sensible strategy to avoid wasting
resources and draining battery from inactive tabs. However in the use case
of a WebRTC-based multiplayer game, the host is effectively acting as the
game server, and this basically hangs the server. If there were 20 peers
connected to the host, the game hangs for all 20 players.
Users regularly switch tabs and probably don't expect that this hangs the
game for everyone. To prevent multiplayer games commonly hanging, perhaps
there could be a new API to request that a page can keep running at full
capacity even in the background, such as
window.requestBackgroundProcessing(). This could show a permission prompt
like 'Do you want to allow this page to run in the background? Allow /
Deny' to help protect against abuse.
The formal definition could be: if background processing is approved, all
timer and requestAnimationFrame callbacks run with the same frequency they
would in "visible" state even when in "hidden" state (using the terms from
the Page Visibility API).
Alternatively browsers could automatically perform this if any WebRTC
DataChannel has been created by the page. However there are probably cases
where it doesn't matter if the DataChannel hangs or reduces to 1 Hz
response, and so it would still be good to save resources. Also, there may
be other use cases not involving WebRTC where it would be useful to run a
page in the background.
If this specific proposal does not seem appropriate, please consider
suggesting any other possible solutions to avoid multiplayer games hanging
when put in to a background tab - we've already run in to this with
testing, so it seems likely to affect real-world games too.
More information about the whatwg