Movatterモバイル変換


[0]ホーム

URL:


Tutorial: Create a WebSocket chat app with a WebSocket API, Lambda and DynamoDB - Amazon API Gateway
DocumentationAmazon API GatewayDeveloper Guide
Step 1: Create Lambda functions and a DynamoDB tableStep 2: Create a WebSocket APIStep 3: Test your APIStep 4: Clean upNext steps: Automate with CloudFormation

Tutorial: Create a WebSocket chat app with a WebSocket API, Lambda and DynamoDB

In this tutorial, you'll create a serverless chat application with a WebSocket API. With a WebSocket API, you can support two-way communication between clients. Clients can receive messages without having to poll for updates.

This tutorial takes approximately 30 minutes to complete. First, you'll use an CloudFormation template to create Lambda functions that will handle API requests, as well as a DynamoDB table that stores your client IDs. Then, you'll use the API Gateway console to create a WebSocket API that integrates with your Lambda functions. Lastly, you'll test your API to verify that messages are sent and received.

Architectural overview of the API that you create in this tutorial.

To complete this tutorial, you need an AWS account and an AWS Identity and Access Management user with console access. For more information, seeSet up to use API Gateway.

You also needwscat to connect to your API. For more information, seeUse wscat to connect to a WebSocket API and send messages to it.

Step 1: Create Lambda functions and a DynamoDB table

Download and unzipthe app creation template for CloudFormation. You'll use this template to create a Amazon DynamoDB table to store your app's client IDs. Each connected client has a unique ID which we will use as the table's partition key. This template also creates Lambda functions that update your client connections in DynamoDB and handle sending messages to connected clients.

To create an CloudFormation stack
  1. Open the CloudFormation console athttps://console.aws.amazon.com/cloudformation.

  2. ChooseCreate stack and then chooseWith new resources (standard).

  3. ForSpecify template, chooseUpload a template file.

  4. Select the template that you downloaded.

  5. ChooseNext.

  6. ForStack name, enterwebsocket-api-chat-app-tutorial and then chooseNext.

  7. ForConfigure stack options, chooseNext.

  8. ForCapabilities, acknowledge that CloudFormation can create IAM resources in your account.

  9. ChooseNext, and then chooseSubmit.

CloudFormation provisions the resources specified in the template. It can take a few minutes to finish provisioning your resources. When the status of your CloudFormation stack isCREATE_COMPLETE, you're ready to move on to the next step.

Step 2: Create a WebSocket API

You'll create a WebSocket API to handle client connections and route requests to the Lambda functions that you created in Step 1.

To create a WebSocket API
  1. Sign in to the API Gateway console athttps://console.aws.amazon.com/apigateway.

  2. ChooseCreate API. Then forWebSocket API, chooseBuild.

  3. ForAPI name, enterwebsocket-chat-app-tutorial.

  4. ForIP address type, selectIPv4.

  5. ForRoute selection expression, enterrequest.body.action. The route selection expression determines the route that API Gateway invokes when a client sends a message.

  6. ChooseNext.

  7. ForPredefined routes, chooseAdd $connect,Add $disconnect, andAdd $default. The$connect and$disconnect routes are special routes that API Gateway invokes automatically when a client connects to or disconnects from an API. API Gateway invokes the$default route when no other routes match a request.

  8. ForCustom routes, chooseAdd custom route. ForRoute key, entersendmessage. This custom route handles messages that are sent to connected clients.

  9. ChooseNext.

  10. UnderAttach integrations, for each route andIntegration type, choose Lambda.

    ForLambda, choose the corresponding Lambda function that you created with CloudFormation in Step 1. Each function's name matches a route. For example, for the$connect route, choose the function namedwebsocket-chat-app-tutorial-ConnectHandler.

  11. Review the stage that API Gateway creates for you. By default, API Gateway creates a stage nameproduction and automatically deploys your API to that stage. ChooseNext.

  12. ChooseCreate and deploy.

Step 3: Test your API

Next, you'll test your API to make sure that it works correctly. Use thewscat command to connect to the API.

To to get the invoke URL for your API
  1. Sign in to the API Gateway console athttps://console.aws.amazon.com/apigateway.

  2. Choose your API.

  3. ChooseStages, and then chooseproduction.

  4. Note your API'sWebSocket URL. The URL should look likewss://abcdef123.execute-api.us-east-2.amazonaws.com/production.

To connect to your API
  1. Use the following command to connect to your API. When you connect to your API, API Gateway invokes the$connect route. When this route is invoked, it calls a Lambda function that stores your connection ID in DynamoDB.

    wscat -c wss://abcdef123.execute-api.us-west-2.amazonaws.com/production
    Connected (press CTRL+C to quit)
  2. Open a new terminal and run thewscat command again with the following parameters.

    wscat -c wss://abcdef123.execute-api.us-west-2.amazonaws.com/production
    Connected (press CTRL+C to quit)

    This gives you two connected clients that can exchange messages.

Step 4: Clean up

To prevent unnecessary costs, delete the resources that you created as part of this tutorial. The following steps delete your CloudFormation stack and WebSocket API.

To delete a WebSocket API
  1. Sign in to the API Gateway console athttps://console.aws.amazon.com/apigateway.

  2. On theAPIs page, select yourwebsocket-chat-app-tutorial API. ChooseActions, chooseDelete, and then confirm your choice.

To delete an CloudFormation stack
  1. Open the CloudFormation console athttps://console.aws.amazon.com/cloudformation.

  2. Select your CloudFormation stack.

  3. ChooseDelete and then confirm your choice.

Next steps: Automate with CloudFormation

You can automate the creation and cleanup of all of the AWS resources involved in this tutorial. For an CloudFormation template that creates this API and all related resources, seews-chat-app.yaml.

WebSocket API tutorials
Tutorial: Create a WebSocket API with an AWS integration

[8]
ページ先頭

©2009-2025 Movatter.jp