Set up route responses for a WebSocket API in API Gateway - Amazon API Gateway
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Set up route responses for a WebSocket API in API Gateway

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 Amazon CLI or an Amazon SDK.

For more information about route response selection expressions, see Route response selection expressions.

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:

  1. Sign in to the API Gateway console, choose a WebSocket API with a proxy Lambda function integration on the $default route.

  2. Under Routes, choose the $default route.

  3. Choose Enable two-way communication.

  4. Choose Deploy API.

  5. Deploy your API to a stage.

Use the following wscat command to connect to your API. For more information about wscat, see Use wscat to connect to a WebSocket API and send messages to it.

wscat -c wss://api-id.execute-api.us-west-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 Amazon CLI

To set up a route response for a WebSocket API using the Amazon CLI, call the create-route-response command as shown in the following example. You can identify the API ID and route ID by calling get-apis and get-routes.

aws apigatewayv2 create-route-response \ --api-id aabbccddee \ --route-id 1122334 \ --route-response-key '$default'

Example output:

{ "RouteResponseId": "abcdef", "RouteResponseKey": "$default" }