admin管理员组文章数量:1389903
I would like to run a web worker in the background (on mobile, it works on desktop) which does an action every minute by sending an HTTP request. Unfortunately, after having the website in the background for about 5 minutes, no HTTP request will be sent anymore... All network requests will resume once you go back to the website.
It seems like the web worker is not allowed to be run in the background, at least not on mobile, as it works fine on desktop. I could not find any source stating why it is throttled, and what can be done about it. I found a similar issue dating from 2017, which is 4 years ago and has not been answered quite the way I want it and might also be outdated.
I thought of the following solutions:
- Normally in native Android you can show a notification which will ensure that your app keeps running in the background, I was wondering if this would also work on web in bination with web workers.
- If you open a web socket in your web worker, will it keep your web worker alive in the background?
- Progressive web app. Does not seem to keep it active unfortunately..
Is there anything I could do about this?
I would like to run a web worker in the background (on mobile, it works on desktop) which does an action every minute by sending an HTTP request. Unfortunately, after having the website in the background for about 5 minutes, no HTTP request will be sent anymore... All network requests will resume once you go back to the website.
It seems like the web worker is not allowed to be run in the background, at least not on mobile, as it works fine on desktop. I could not find any source stating why it is throttled, and what can be done about it. I found a similar issue dating from 2017, which is 4 years ago and has not been answered quite the way I want it and might also be outdated.
I thought of the following solutions:
- Normally in native Android you can show a notification which will ensure that your app keeps running in the background, I was wondering if this would also work on web in bination with web workers.
- If you open a web socket in your web worker, will it keep your web worker alive in the background?
- Progressive web app. Does not seem to keep it active unfortunately..
Is there anything I could do about this?
Share Improve this question asked Mar 26, 2021 at 18:38 JasonJason 1,6983 gold badges23 silver badges51 bronze badges 2- what if u use WebSocket. would it be too much? – ashen madusanka Commented Apr 5, 2021 at 6:22
- I might be able to try out websockets, though I haven't had the time yet and I also think it would be a specific work-around rather than helping other people having the same problem – Jason Commented Apr 6, 2021 at 15:27
1 Answer
Reset to default 7 +50All modern browsers restrict background usage. It has a really simple reason: Background tasks require resources, and users dont want a million websites in the background to eat all your RAM. And malicious websites could just use the CPU from users for bitcoin mining etc.
But there is a way to do stuff in the background. You already mentioned it in the question: You need to send push notifications. You can just include a fetch()
in your push notification handler.
But here's the catch: You have to send a notification every time you want your site to fetch something, or your requests will always/sometimes be blocked depending on your browser. From MDN:
Activating a service worker to deliver a push message can result in increased resource usage, particularly of the battery. Different browsers have different schemes for handling this, there is currently no standard mechanism. Firefox allows a limited number (quota) of push messages to be sent to an application, although Push messages that generate notifications are exempt from this limit. The limit is refreshed each time the site is visited. In parison, Chrome applies no limit, but requires that every push message causes a notification to be displayed.
You just need to set up Push Notifications. There's a great guide from Google that you can follow if you don't know how to set up push notifications.
An implementation in a service worker would look like this:
self.addEventListener('push', function(event) {
if (!(self.Notification && self.Notification.permission === 'granted')) {
return;
}
// The HTTP request
fetch("...");
var data = {};
if (event.data) {
data = event.data.json();
}
var title = data.title || "Background activity";
var message = data.message || "You can ignore this";
var icon = "images/new-notification.png";
var notification = new Notification(title, {
body: message,
tag: 'simple-push-demo-notification',
icon: icon
});
notification.addEventListener('click', function() {
if (clients.openWindow) {
clients.openWindow('https://example.blog./2015/03/04/something-new.html');
}
});
});
(Code copied from MDN)
本文标签: javascriptRun a persistent web worker in the background without getting throttledStack Overflow
版权声明:本文标题:javascript - Run a persistent web worker in the background without getting throttled - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744654653a2617891.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论