WebSocket routes can be configured for two-way or one-way communication. API Gateway will not pass the backend response through to the route response, unless you set up a route response.
Note
You can only define the $default
route response for WebSocket APIs. You can use an integration
response to manipulate the response from a backend service. For more information, see Overview of
integration responses.
You can configure route responses and response selection expressions by using the API Gateway console or the AWS CLI or an AWS SDK.
For more information about route response selection expressions, see Route response selection expressions.
Topics
Set up a route
response using the API Gateway console
After you have created a WebSocket API and attached a proxy Lambda function to the default route, you can set up route response using the API Gateway console:
-
Sign in to the API Gateway console, choose a WebSocket API with a proxy Lambda function integration on the
$default
route. -
Under Routes, choose the
$default
route. -
Choose Enable two-way communication.
-
Choose Deploy API.
-
Deploy your API to a stage.
Use the following wscatwscat
, see Use wscat to
connect to a WebSocket API and send messages to it.
wscat -c wss://
api-id
.execute-api.us-east-2
.amazonaws.com/test
Press the enter button to call the default route. The body of your Lambda function should return.
Set up a route
response using the AWS CLI
The following create-route-response command creates a route response for the
$default
route. You can identify the API ID and route ID by using the get-apis and get-routes commands.
aws apigatewayv2 create-route-response \ --api-id
aabbccddee
\ --route-id1122334
\ --route-response-key '$default'
The output will look like the following:
{ "RouteResponseId": "abcdef", "RouteResponseKey": "$default" }