Dev.to
7/21/2026

Making a WebSocket survive Chrome's Manifest V3
Short summary
A detailed engineering post on keeping WebSockets alive inside Chrome Manifest V3 extensions, where service workers are terminated after ~30s of idle time. The fix involves three parts: a 20-second heartbeat to keep the worker active, auto-reconnect that pulls a fresh state snapshot so drops are invisible to users, and try/catch around every port message since the worker can die mid-operation. The article includes concrete code examples and explains the CSP and lifecycle constraints that led to the bug.
- •Manifest V3 service workers terminate after ~30s idle, killing any WebSocket living in them
- •Fix requires heartbeat to reset idle timer, auto-reconnect with state snapshot, and defensive try/catch on port messages
- •Content scripts can't open WebSockets on GitHub due to strict CSP in Firefox, forcing socket into the background worker
Generated with AI, which can make mistakes.
Is this a good recommendation for you?



