How can I keep a WebSocket connection active in my PWA when the screen is locked?
Asked by
TechieExplorer92
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?
Answers
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.
Reply to this answer
Add your answer
Answers are reviewed before publication.
AudioGuru77: 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.