Update: We have released an updated and more feature-rich Websocket chat application sample using AWS CDK that you can find here.
This is the code and template for the simple-websocket-chat-app
.
There are three functions contained within the directories and a SAM
template that wires them up to a DynamoDB
table and provides the minimal set of permissions needed to run the app:
.
├── README.md <-- This instructions file
├── onconnect <-- Source code onconnect
├── ondisconnect <-- Source code ondisconnect
├── sendmessage <-- Source code sendmessage
└── template.yaml <-- SAM template for Lambda Functions and DDB
You have two choices for how you can deploy this code.
The first and fastest way is to use AWS's Serverless Application Repository to directly deploy the components of this app into your account without needing to use any additional tools. You'll be able to review everything before it deploys to make sure you understand what will happen. Click through to see the application details.
If you prefer, you can install the AWS SAM CLI and use it to package, deploy, and describe your application. These are the commands you'll need to use:
sam deploy --guided
aws cloudformation describe-stacks \
--stack-name simple-websocket-chat-app --query 'Stacks[].Outputs'
Note: .gitignore
contains the samconfig.toml
, hence make sure backup this file, or modify your .gitignore locally.
To test the WebSocket API, you can use wscat, an open-source command line tool.
$ npm install -g wscat
$ wscat -c wss://{YOUR-API-ID}.execute-api.{YOUR-REGION}.amazonaws.com/{STAGE}
$ wscat -c wss://{YOUR-API-ID}.execute-api.{YOUR-REGION}.amazonaws.com/prod
connected (press CTRL+C to quit)
> {"action":"sendmessage", "data":"hello world"}
< hello world
This sample code is made available under a modified MIT license. See the LICENSE file.