I'm developing a Progressive Web App (PWA) that plays audio, and I need the server to tell it when to switch to a new sound. However, I'm struggling with how to maintain the WebSocket connection when the user's screen locks. I know that native apps handle this better, but what about PWAs? I can't find much documentation on this topic. I'm aware that mobile browsers have different limitations for PWAs and tend to pause JavaScript and network activities aggressively when the app is backgrounded. So, is there a way to keep a WebSocket connection alive in a PWA when the screen is locked? What minimum requirements would I need for both Android and iOS to manage this?
1 Answer
Unfortunately, keeping a WebSocket connection alive in a PWA when the screen is locked isn't very reliable. When the screen locks, mobile browsers typically pause JavaScript and network activity quite aggressively. If you need consistent server-driven updates while locked, that’s more in the native app realm.

You might want to consider using Cordova. That way, you can keep most of your app's functionality as a web app and leverage native capabilities just for background audio.