B. Create the Websocket Routes

Next we will create the routes for your Websocket API. In your WebSocket API, incoming JSON messages are directed to backend integrations based on routes that you configure. (Non-JSON messages are directed to a $default route that you configure.)

There are three predefined routes that can be used: $connect, $disconnect, and $default. In addition, you can create custom routes.

Click here to learn more about Websocket routes.

To start, navigate to the Routes page of your API (you may be there already).

Create the connect route:

API Gateway calls the $connect route when a persistent connection between the client and a WebSocket API is being initiated.

  1. Click on the $connect route.
  2. Make sure Lambda Function is selected for Integration Type.
  3. Make sure Lambda Proxy Integration is selected.
  4. Enter <YourEnvironmentName>WebSocketConnect for Lambda Function.
  5. For Execution Role enter the Role ARN for the IAM Role <YourEnvironmentName>API (You can find this Role ARN by searching for the role in the IAM Service Console).
  6. Click Default Timeout.
  7. Select Save.
  8. Select Ok to move past any pop-ups.

Create the start-game route:

  1. Enter start-game in, New Route Key and click the check box to the right.
  2. Make sure Lambda Function is selected for Integration Type.
  3. Make sure Lambda Proxy Integration is selected.
  4. Enter <YourEnvironmentName>WebSocketSynchronizeStart for Lambda Function.
  5. For Execution Role enter the Role ARN for the IAM Role <YourEnvironmentName>API (You can find this Role ARN by searching for the role in the IAM Service Console).
  6. Click Default Timeout.
  7. Select Save.
  8. Select Ok to move past any pop-ups.

For the Disconnect route:

API Gateway calls the $disconnect route when the client or the server disconnects from the API.

  1. Click on the $disconnect route.
  2. Make sure Lambda Function is selected for Integration Type.
  3. Make sure Lambda Proxy Integration is selected.
  4. Enter <YourEnvironmentName>WebSocketDisconnect for Lambda Function.
  5. For Execution Role enter the Role ARN for the IAM Role <YourEnvironmentName>API (You can find this Role ARN by searching for the role in the IAM Service Console)
  6. Click Default Timeout.
  7. Select Save.
  8. Select Ok to move past any pop-ups.