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

fix unreasonable room user information status #27

Merged
merged 2 commits into from
Nov 28, 2024

Conversation

WinterJack002
Copy link
Contributor

When a user who originally joined a room exits due to some interruption (like a page refresh or browser closure), their status still appears online to other users in the room(like the picture), which doesn't make sense. Additionally, the Redis database will perpetually retain this "frozen" stream, resulting in a waste of resources.

图片

Add 'useEffect' listen interruption and release resources.

Copy link
Member

@a-wing a-wing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW: When beforeunload use http request will display cancel in console

Screenshot 2024-11-27 at 23 20 59

Maybe should set keepalive: true

https://stackoverflow.com/questions/63157089/sending-post-request-with-fetch-after-closing-the-browser-with-beforeunload

}
// Need to set separate Browser Events Parameters for Firefox refresh and browser termination
window.addEventListener('beforeunload', cleanup) // Used to monitor the browser termination of Firefox event
window.addEventListener('unload', cleanup) // Used to monitor Firefox refresh event
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refresh action can send event beforeunload

Why separate refresh and termination?

unload event is Deprecated https://developer.mozilla.org/en-US/docs/Web/API/Window/unload_event

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see carefully.

  • Test result:

Based on my testing results in the development mode on my local machine: The beforeunload event is sufficient for Chrome , meaning there's no need to add a keepalive request. However, Firefox behaves differently. When refreshing the page, it doesn't trigger the callback function of the beforeunload event, and when closing the window, it doesn't trigger the 'unload' event. That's why I'm using both of them here.

unload event is Deprecated, but still work in Firefox.

Most importantly, I tried the keepalive solution, but it seemed ineffective.

  • Possible reason:

By the way, I found these introductions to beforeunload on MDN Web Docs, and I think it may be related to Woom's SPA design.

图片
https://developer.mozilla.org/en-US/docs/Web/API/Window/beforeunload_event

  • Test environment:

User-Agent:
Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:132.0) Gecko/20100101 Firefox/132.0

  • Personal suggestion:

So, in a word: Perhaps this is the simplest way to solve this problem:

    window.addEventListener('unload', cleanup) // used to monitor Firefox refresh event

or we can change delStream() by using navigator.sendBeacon maybe? how do u think?

Looking forward to your opinion.

  • Personal understanding:

The 'beforeunload' is used to perform callback BEFORE the page is unloaded, but it can prevent some asynchronous operations (like network requests) from completing within the event. This is why DELETE requests sometimes show as "canceled" in the console. That's why using the 'unload' event allows the request to trigger AFTER the interface is unloaded (or refreshed).

When close a browser window, the browser doesn't immediately interrupt all activities. Instead, it tries to complete asynchronous operations (like sending network requests) before the window is closed. That's why the 'beforeunload' event works when close the window.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user-agent:
Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OS: MacOS 15.1 (24B83) (M3 Pro)
Chrome: 130.0.6723.70
Friefox: 132.0.2 (aarch64)
Safari: 18.1 (20619.2.8.11.10)

event Chrome Firefox Safari
beforeunload ok console error, no request console error, request ok
unload console error, no request ok console no request log, no request
beforeunload + keepalive ok console error, no request console error, request ok
unload + keepalive console error, request ok ok console no request log, request ok

@a-wing a-wing added bug Something isn't working mod::webapp In browser labels Nov 28, 2024
@a-wing a-wing merged commit 409ad2a into binbat:main Nov 28, 2024
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working mod::webapp In browser
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants