feat: Add resetPusherInstance function #110
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixing issue when pusher Instance is initialized multiple times
Problem
We encountered a critical bug in the project where initializing the Pusher instance multiple times for different users led to the registration of handlers/listeners multiple times. This caused unexpected behavior and performance degradation as well as crashes on android. The root cause was the absence of a mechanism to reset the Pusher instance and clear all the previous listeners and subscribed channels when a different user logged in.
Solution
To address this issue, I've introduced a new public function named
resetPusherInstance
to the existing class. This function is responsible for performing the necessary cleanup tasks before initializing the Pusher instance for a new user session. Here's what the solution entails:New Public Function: Added a new public function
resetPusherInstance
to the class. This function encapsulates the logic to unsubscribe from all the previously subscribed channels and remove all the listeners/handlers associated with the current Pusher instance.Usage during User Switch: Whenever a different user logs in, the
resetPusherInstance
function is invoked to ensure that any previous listeners and subscriptions are removed before initializing a new Pusher instance.Preventing Redundant Listeners: With this change, we prevent the issue of redundant listeners being registered due to multiple Pusher instance initializations.
How This Fixes the Issue
By incorporating the
resetPusherInstance
function into the class, we provide a reliable way to reset the Pusher instance's state and prevent the accumulation of redundant listeners and subscriptions. This ensures that each user session starts with a clean slate, mitigating the problem of multiple initialization-related bugs.Testing
I've thoroughly tested this solution by simulating various scenarios of user logins and Pusher instance initializations. The tests demonstrate that the
resetPusherInstance
function effectively clears previous state and prevents the bug from occurring.Changes Made
resetPusherInstance
function to the existing class.Impact
This contribution not only addresses the bug we encountered but also improves the overall stability and performance of the project when dealing with multiple user sessions.
Additional Notes
I'm open to feedback and suggestions for further refinement of this solution. Thank you for considering this pull request.