- Notifications
You must be signed in to change notification settings - Fork3k
AddshouldReconnectAfterClose to js Socket#5155
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Open
doorgan wants to merge1 commit intophoenixframework:mainChoose a base branch fromdoorgan:master
base:main
Could not load branches
Branch not found:{{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline, and old review comments may become outdated.
Uh oh!
There was an error while loading.Please reload this page.
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
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.
I'm experiencing an issue that is hard/ugly to address with the current Socket implementation.
We have an application using phoenix channels, and we perform authentication on socket connection with a phoenix token. That is, we don't let the user join a channel if the provided token is expired or invalid.
This works well, and the server properly responds with a
401 Unauthorizedresponse. By using theerror_handleroption for thePhoenix.SocketI can also include an error reason in the response body, and that works well too: the server is behaving correctly.However, the js WebSocket client completely ignores that response, and produces a
CloseEventwith code1006and an empty reason. This is a well known issue, as explained here:https://stackoverflow.com/a/19305172. To make things worse, the very same close event is fired when the connection fails due to the server being down.This means that if the connection was refused by the server, the js client will keep trying to reconnect indefinitely, polluting our logs and metrics with a lot of noise, and currently there's no way to stop it from reconnecting other than subclassing the
Socketand relying on the class internals to change theonConnClosebehavior.For our use case, we want to know why the connection was refused to provide feedback to the user. The connection can be lost briefly during a deploy, or due to the user having the browser tab open longer than the token lifetime(this is super common for our users), and we want to provide different feedback depending on the case. We can figure out the reason without the event, we only need a way to stop the reconnections.
This PR adds a function that gives us a chance to stop the socket from reconnecting after a close event.
PS: if this is accepted, I'm wondering if it could be backported to phoenix 1.6