-
-
Notifications
You must be signed in to change notification settings - Fork 796
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
feat: Ensure websocket
parity with API Gateway
#1301
Conversation
cc: @coulson84 as you were commenting on the other PRs, comments/questions/issues? cc: @VenomAV @kevbot-git thoughts on this to replace your existing PRs? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unless the aws websockets API gateway setup has changed since the first quarter I am fairly certain that there's no response sent on the $connect or $disconnect routes - it looks like this PR only doesn't send request body on the $disconnect route. Not sure why aws
implemented that way but I am reasonably sure that this is still the case.
with my recent evaluations of AWS, you are correct, there is no response on $connect or $disconnect
|
Also for clarity, I leveraged What I noticed before was Serverless Offline would blindly let the socket handshake and connect which is not the same as API Gateway V2. Here's the relevant parts from WebSocketServer in |
@coulson84 hey Joe! any feedback on my responses to your review comment? thanks! |
Hi, just this I think: where there is a check just before sending the lambda response body back down to the client, it checks if the route is $disconnect and then does not send the body if that is the case. I think that check should also include the $connect route. As per your and my comments previously there is no response sent in $connect routes. If that's handled elsewhere I apologise. I'm looking through things on my phone which is never ideal. |
hey @coulson84 thanks for the feedback!
|
Yes, sounds right to me. I thought I must've missed where you had implemented it as you said it was correct to not send down the connect response but I didn't see where. 👍 |
@coulson84 awesome thanks! whats the process to get this merged and released? |
@coulson84 hey there! whats next in getting this merged? build status is currently "awaiting approval" for first time contributors |
Alas, I hold no power here. @dherault can you comment? |
@coulson84 if you could click the Approve button on the PR that'd help things along! pinging: @Bilal-S @daniel-cottone @leonardoalifraco @mikestaub as project maintainers, whats the process to get this merged |
@mikestaub thanks for the approval! can we get the workflow unblocked and get this merged? thanks ! |
Actually developing against @cnuss branch. This is a must have for who use API Gateway with Websockets |
@pgrzesik hey there! do you have the ability to approve workflows |
Hey @cnuss - I do but for some reason no workflows have been triggered here - could you please create a new PR or try to push to this one again? I'm not sure what's going on |
@pgrzesik thanks! just sync'd with dherault/serverless-offline@master, and it's still blocked... who are the maintainers on this project? according to GH docs, they should see a "Approve and Run" button if they are a privileged maintainer on this repo |
I just clicked the approve, it was not available previously @cnuss |
@pgrzesik thanks for the run approval! looks like there's green checks across the board, can we get this merged? and whens the next release planned? |
I have an additional question @cnuss - do you think it would be possible to cover the functionality you're adding with some automated tests? I'm unfortunatelly not familiar with the websockets part of this codebase and I'm not confident in accepting the changes. |
@pgrzesik i can throw something together, of course! |
@pgrzesik added tests, could you approve the workflow again? |
@pgrzesik looks like we're green across the board, went into the logs to ensure that my tests did in fact run and pass! FYI: it doesn't appear the tests are run against a real AWS account so i took the liberty to run them against my own AWS account: two-way
one-way
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks a lot @cnuss - it looks great 👍
websocket
parity with API Gateway
Description
This PR gives
serverless-offline
handling of Web Sockets parity with how AWS API Gateway V2 handles Web SocketsDiscrepancies
Status Code Handling
When a client connects, the Lambda Handler (or Authorizer) can set a
statusCode
to control whether the connection is allowed or not.AWS API Gateway:
wscat
command produces the following error:error: Unexpected server response: {statusCode}
Serverless Offline:
statusCode
is ignoredrouteResponseSelectionExpression
From the AWS Docs, a
routeResponseSelectionExpression
(of value$default
) can be placed on the$connect
handler to enable 2-way communication for websockets.AWS API Gateway:
body
to the client.Serverless Offline:
routeResponseSelectionExpression
option inserverless.yml
Motivation and Context
Most notably, the current implementation of Web Socket handling is a
TODO
, and this should remedy theTODO
to make behavior similar to AWS.serverless-offline/src/events/websocket/WebSocketClients.js
Line 147 in 2ac804b
This issue has been raised before in two PRs, and this PR likely supersedes them:
Fixing connections
Using
WebSocketServer.verifyClient
, initial connections now invoke the lambda$connect
handler, and if the status code is non-2xx, it will properly abort the handshake with thestatusCode
from the lambda response.This results in the same abort handling as AWS API Gateway, when running offline:
Local:
Remote:
Two-way responses
If the optional
routeResponseSelectionExpression
has been specified the content ofbody
will be sent back to the websocket.How Has This Been Tested?
I have the following service running in Lambda:
https://github.com/scaffoldly-demo/cool-sls-rest-api
The
$connect
handler takes thestatusCode
query parameter and returns it as a number to simulate response code handling. 2xx's allow the connection to go through.serverless.yml
has also been outfitted withrouteResponseSelectionExpression
and responses are returned when the setting is enabled