Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

196
Views
Is there a way for SharedWorker to tell if it is about to get closed?

Is there a way to tell that the SharedWorker is about to get closed, something like the beforeunload event that window has?

I need this information inside of the worker to signal server of closing of web socket etc.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

No.

But SharedWorker will be alive as long as there is any live page holding a reference to it (through new SharedWorker()). So what you can do is to post a message to your SharedWorker when your page is about to go away (pagehide event), and your SharedWorker can keep track of how many pages are alive and close the WebSocket when the live count drops to zero.

However, do watch out for the definition of "live page". Chrome (and other browsers as well) tries very hard to optimize the browser performance and energy impact. Chrome has this Tab Discarding feature where it will discard background pages when there is pressure on the memory. Discarding a page will throw away the page data without closing the window, so that when user clicks the windows again Chrome can reload the page. This usually isn't an issue for simple website, but given that you are running websocket inside SharedWorker this could have some implications on your overall protocol design. In particular, if all of your pages are discarded, your SharedWorker dies as well. And discarding a page has absolutely zero notification.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!