I am trying to setup an extremely simple WebSocket mock within AWS ApiGateway. However, every attempt I've tried gives me an error:
13:36:52 (X33uOGUfIAMFq7w=) Extended Request Id: X33uOGUfIAMFq7w=
13:36:52 (X33uOGUfIAMFq7w=) Verifying Usage Plan for request: X33uOGUfIAMFq7w=. API Key: API Stage: redacted/prod
13:36:52 (X33uOGUfIAMFq7w=) API Key authorized because route '$connect' does not require API Key. Request will not contribute to throttle or quota limits
13:36:52 (X33uOGUfIAMFq7w=) Usage Plan check succeeded for API Key and API Stage redacted/prod
13:36:52 (X33uOGUfIAMFq7w=) Starting execution for request: X33uOGUfIAMFq7w=
13:36:52 (X33uOGUfIAMFq7w=) WebSocket Request Route: [$connect]
13:36:52 (X33uOGUfIAMFq7w=) Client [UserAgent: null, SourceIp: redacted] attempts to connect to WebSocket API [redacted].
13:36:52 (X33uOGUfIAMFq7w=) Execution failed due to configuration error: statusCode should be an integer which defined in request template
13:36:52 (X33uOGUfIAMFq7w=) Client [UserAgent: null, SourceIp: redacted] failed to connect to API [redacted].
As far as I can tell, I've followed the most basic configuration possible. I do not need any responses or templates, just simple a WebSocket connection that allows me to connect to it and do nothing, or perhaps respond to ping
requests with a pong
eventaully.
All authorizations and API keys are disabled. No request templates or integration responses are setup since I do not need them. Again, the goal is to just be able to have a dumb WebSocket that can be connected to.
However, whenever I try to connect to it, I get a 500 error with the error logs from above about a status code.
WebSocket connection to 'wss://redacted.execute-api.us-east-1.amazonaws.com/prod' failed: Error during WebSocket handshake: Unexpected response code: 500
To create a WebSocket API using the API Gateway consoleSign in to the API Gateway console and choose Create API. Under WebSocket API, choose Build. Under Settings, in the API name field, enter the name of your API, for example, PetStore . Enter a Route Selection Expression for your API, for example, $request.
A WebSocket API in API Gateway is a collection of WebSocket routes that are integrated with backend HTTP endpoints, Lambda functions, or other AWS services. You can use API Gateway features to help you with all aspects of the API lifecycle, from creation through monitoring your production APIs.
Set up a WebSocket API integration request using the API Gateway console. Sign in to the API Gateway console, choose the API, and choose Routes. Under Routes, choose the route. In the Route Overview pane, choose Integration Request.
Using the following sources (below), I was able to get a quick and loose example AWS API Gateway connection working use MOCK endpoints with web sockets. I'm sure this can be flushed out more to provide an even better understanding of different request/response body messages and codes..
$request.body.message
for the route selection expression
$connect
routeClick on the Template Key 200
, and enter a template: {"statusCode" : 200}
and then click the Save button.
Select Route Overview to go back to the $connect
overview page and then select the Add Integration Response button
Create a response key of $default, which will reference the $default
route under Routes
For the $disconnect
route, please repeat steps 7 - 13 for this specific route.
$disconnect
route, please select the $default
route under the Routes pane.
$connect
and $disconnect
(steps 7 - 11)$default
, we will be setting the route response up slightly different than $connect
and $disconnect
as those 2 routes' responses actually reference the $default
response within their own respective Integration Responses.200
, and enter a template: {"statusCode" : 200, "connectionId" : "$context.connectionId"}
and then click the Save button.
{"connectionId" : "$context.connectionId"}
was added to the response payload to show how variables can be used within the response to represent an actual value of the connection.wss://#######.execute-api.us-east-1.amazonaws.com/Test
)wscat
via cli, I am now able to connect to my WebSocket URL (see source below for installing wscat): wscat -c wss://#######.execute-api.us-east-1.amazonaws.com/Test
Sources:
https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-how-to-call-websocket-api-wscat.html
https://forums.aws.amazon.com/thread.jspa?threadID=299642&tstart=0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With