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

notificationClient: Establish ws connection only on client render #1460

Merged
merged 2 commits into from
Jul 3, 2023

Conversation

sashko9807
Copy link
Member

@sashko9807 sashko9807 commented Jul 1, 2023

Closes #1454

Motivation and context

The current websocket configuration, creates a new websocket client, for every SSR generated page. Since those clients are technically active, they are never cleaned up, thus resulting in memory leak. This change makes it so, a new web socket connection is created only on first render of the component.

Testing

Steps to test

  1. Start visiting different pages randomly
  2. Observe the current websocket clients, in the Socket object (you can console.log the client arg from handleConnection) . The total amount of active clients can be found in Socket.adapters.sids
  3. The size of sids should remain 1, per browser window

The current websocket configuration, creates a new websocket client, for every SSR generated page. Since those clients are technically active, they are never cleaned up, thus resulting in memory leak.
This change makes it so, a new web socket connection is created only on first render of the component.

Tests:
1. Start visiting different pages randomly
2. Observe the current websocket clients, in the Socket object. They can be found in Socket.adapters.sids
3. The size of sids should remain 1, per browser window
@github-actions
Copy link

github-actions bot commented Jul 1, 2023

✅ Tests will run for this PR. Once they succeed it can be merged.

Copy link
Contributor

@slavcho slavcho left a comment

Choose a reason for hiding this comment

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

I thought there was no problem here, but I was wrong...
It reproduces when you start clicking on multiple pages.

This actually fixes the issue #1454. I've tested it.

@imilchev
Copy link
Contributor

imilchev commented Jul 1, 2023

can we also make sure that connections are kept open only if you have the window focused? This means unless podkrepi.bg is opened in a non-minimized browser window, there should be no socket connection open.

You can check this. What I am suggesting is to open and close socket connections based on the visibilityState. I am fine with doing that in a follow-up PR too

@sashko9807 sashko9807 force-pushed the socket-io-connection branch from ad326ae to 415d5af Compare July 1, 2023 13:25
@sashko9807
Copy link
Member Author

can we also make sure that connections are kept open only if you have the window focused? This means unless podkrepi.bg is opened in a non-minimized browser window, there should be no socket connection open.

You can check this. What I am suggesting is to open and close socket connections based on the visibilityState. I am fine with doing that in a follow-up PR too

Done I think.
Should temporary switched off donation notification popups be reverted in this PR, or this will be handled in another one?

@imilchev
Copy link
Contributor

imilchev commented Jul 1, 2023

Let's flip the switch in another PR

@imilchev
Copy link
Contributor

imilchev commented Jul 1, 2023

Great work on this! This should help a lot with the resource usage for those websockets 🚀

@imilchev imilchev added the run tests Allows running the tests workflows for forked repos label Jul 1, 2023
@github-actions github-actions bot removed the run tests Allows running the tests workflows for forked repos label Jul 1, 2023
@igoychev igoychev added the run tests Allows running the tests workflows for forked repos label Jul 3, 2023
@github-actions github-actions bot removed the run tests Allows running the tests workflows for forked repos label Jul 3, 2023
Copy link
Contributor

@igoychev igoychev left a comment

Choose a reason for hiding this comment

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

great work for protecting from excessive connections!!!

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

Successfully merging this pull request may close these issues.

[Socket.io]Check connection count
4 participants