API Gateway WebSocket close with status code / reason

0

The WebSocket specification describes that a close code & reason may be specified when closing a WebSocket. The MDN docs detail how the JavaScript WebSocket API allow these as options to be passed to the close method as:

ws.close(code, reason);

Working with the API GateWay WebSockets we have the "delete connection" @connections command to close an open WebSocket, which can be called via the various language SDKs or the CLI delete-connection command. But there are no options to specify the close status code / reason.

I am working with a WebSocket API specification, graphql-ws, which makes use of these status codes.

A similar question was asked on GitHub here.

Is there anything I'm missing, or is this functionality planned in the future?

2 Answers
1

Hi,

I believe that you want to use the WebSocket API integration response in API Gateway: see https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-integration-responses.html on how to set it up properly.

Best,

Didier

profile pictureAWS
EXPERT
answered a month ago
profile picture
EXPERT
reviewed a month ago
0

Based on the available information, it appears that the API Gateway WebSocket API does not currently support specifying a custom close code or reason when closing a WebSocket connection.

The API Gateway Management API's delete-connection command, which is used to disconnect a client from a WebSocket API, does not provide options for specifying a close code or reason. This limitation exists across the various AWS SDKs and the AWS CLI.

While the WebSocket specification and client-side APIs (like the JavaScript WebSocket API) allow for specifying close codes and reasons, this functionality is not currently available in Amazon API Gateway's WebSocket implementation.

For your specific use case with the graphql-ws protocol, which makes use of these status codes, you may need to consider alternative approaches. One possible workaround could be to send a custom message to the client with the desired close code and reason before actually closing the connection. However, this would not be a standard WebSocket close frame with the specified code and reason.

As for future plans, there's no publicly available information indicating whether AWS intends to add this functionality to API Gateway WebSockets. If this feature is important for your use case, you might want to submit a feature request to AWS support or through their feedback channels.

Given the current limitations, you may need to explore alternative solutions or adjust your implementation to work within the constraints of API Gateway's WebSocket capabilities.
Sources
API Gateway Management API examples using AWS CLI - AWS SDK Code Examples
Use @connections commands in your backend service - Amazon API Gateway
Amazon API Gateway Features | API Management | Amazon Web Services

profile picture
answered a month ago
profile pictureAWS
EXPERT
reviewed a month ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions