Throttled background tabs and the case for elevator musicby videoinu authors . published on under technical, dont-take-this-too-seriously .
It's a sunny evening with light rain outside. You catch a glimpse of the inviting outside world while sitting comfortably lodged with a laptop, but with no desire to go outside. Not because of the lockdowns, but because you're too busy staring at the beautiful web app creation you just made.
I need to do a slow computation, but I can just do it in the background while the user is browsing or whatever!, you think to yourself,
This solves all my user-retention problems.
One roadblock: as you would have soon found out, browsers are very aggressive about throttling anything happening in the background tabs. For sure it's a good thing, since we wouldn't want the background cryptominers or even just news websites with more than two ads to siphon all our electricity. Unfortunately, our app heavily relies on this throttling behavior to not be there, and the mere existence is severely hurting our user retention projections. Can we convince the browser that we are the one exception to the throttling rule and have the the limitations be gone with the wind?
Worry not, for the answer lies within the MDN documentation:
Firefox 50 no longer throttles background tabs if a Web Audio API AudioContext is actively playing sound (thankfully, we can generalize this to Chrome and hopefully other browsers too). So, by playing some music in the background tab, the user will both
- have a more pleasant browsing experience in the other tabs (c'mon, who doesn't like involuntary background music?)
- we will have our background calculations processed in no time.
We had a problem, now we have a solution, and all we need anymore is some exciting music to play for the user waiting. And what could be more exciting than elevator music?
Finding the right music was a breeze; our new friend Ben from bensound (who we found from one of the first results when searching for
elevator music royalty free) is here to help with some sick Brazil-inspired Bossa Nova Beats. I dug deep for the download link, discovered the mp3, and put everything together to demonstrate this monstrosity with a nifty timer that increments every centisecond:
To test it out, press "start counting" and switch tabs. The counter (displayed in the tab title) should be counting up, but slowly and infrequently. Then switch back to this tab, press "turn on the music", switch to another tab again, and observe the magic ✨. We've solved our throttling problems.
While we've achieved what we came for with some unorthodox techniques, maybe the real lesson here wasn't about elevator music or even about avoiding throttling. Maybe the real lesson was about properly reading the documentation. The next sentence after "background tabs not being throttled with audio playing" is
Firefox 51 further amends this such that background tabs are no longer throttled if an AudioContext is present in the tab at all, even if no sound is being played, therefore rendering our efforts void.
Oh well, one more feature to toss into the competitor feature comparison table.