[Socket] send connect error to client and close #4886
Draft
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.
This is a draft solution for the discussion at https://elixirforum.com/t/websocket-connection-validation-with-phoenix-channels/48831
Previously if socket connect handler returns an error
then websocket upgrade would be rejected.
Browser websocket api doesn't support getting the status code on upgrade
failure.
This instead successfully upgrades to websocket then sends the error as
the first message and immediately closes the socket.
This is done instead of sending the error in the close frame payload
because that payload is limited to 123 bytes which won't work well for
errors provided by the application developer.
Close status code 3000 is chosen because RFC6455 states
This change allows the application developer to return a custom error on
socket connect to indicate for example an authentication/authorization
error.
The client can then implement custom behaviour based on the error.
See example client usage at https://github.com/BrendanBall/phoenix_socket_test/blob/master/assets/js/user_socket.js#L68.
If this change is acceptable then I can look at making this change in behaviour optional to prevent potential breaking changes with existing clients