Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consider Opening the WebSocket Connection on pageshow and close it on beforeunload to allow BFCache #5232

Open
gilbertococchi opened this issue Nov 19, 2024 · 3 comments
Labels
question Further information is requested

Comments

@gilbertococchi
Copy link

gilbertococchi commented Nov 19, 2024

Similar to this issue: #1660

All pages using socket.io cannot benefit of BFCache on Chrome because of the way the WebSocket Connection is established/closed.

Many sites could benefit of this speed improvements, therefore it would be ideal if this library would be open to:

  1. Open the WebSocket connection at pageshow.

  2. Close the WebSocket connection using beforeunload.

  3. should be done by using pagehide but there is a Chrome bug that would make the change not effective enough: https://issues.chromium.org/issues/360183659

@gilbertococchi gilbertococchi added the enhancement New feature or request label Nov 19, 2024
@darrachequesne
Copy link
Member

Hi!

Shouldn't it be the duty of the user of the library? Something like:

const socket = io({
  autoConnect: false
});

addEventListener("pageshow", (event) => {
  socket.connect();
});

addEventListener("beforeunload", (event) => {
  socket.disconnect();
});

Note: with the closeOnBeforeunload option set to true (defaults to false), the browser will automatically close the WebSocket connection when the beforeunload event is emitted.

@darrachequesne darrachequesne added question Further information is requested and removed enhancement New feature or request labels Nov 21, 2024
@gilbertococchi
Copy link
Author

gilbertococchi commented Nov 21, 2024

Hi Damien, nice to e-meet you and thanks for your quick turn around here!

I agree with you that in theory it should be the developers using the library to use it in the proper way, but It's also likely that most of them may miss this point and sacrifice their page load performance.

Does the Socket.io documentation have a section where perhaps this practice can be suggested as best practice?

Something like, to make sure the WebSocket Connection would not prevent BFCache (that could be a big deal for Performance) make sure to call connect() on the pageshow event.

About the closeOnBeforeunload setting that you mention, is there a reason why the default is non true instead of false?

Actually, once this bug will be fixed, I would much rather suggest using pagehide instead of beforeunload for the same purpose but more reliable and less problematic event.

@gilbertococchi
Copy link
Author

fixed bug link.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants