This repository is a Rust implementation of aws-samples/simple-websockets-chat-app.
The execution environment is assumed to be provided.al2 (Amazon Linux 2)/arm64. Therefore, the default build target is aarch64-unknown-linux-musl
.
If you are building on macOS, use FiloSottile/homebrew-musl-cross or emk/rust-musl-builder.
Packages are built for each of API Gateway's default websocket routes and custom routes.
# Build all packages
cargo build --release --workspace
# Build per package
cargo build --release --package <package_name>
Set the following environment variables.
The environment variables can also be obtained from the WebSocket URL (no need to set AWS_REGION
).
wss://{AWS_API_ID}.execute-api.{AWS_REGION}.amazonaws.com/{AWS_STAGE}
AWS_API_ID
AWS_STAGE
DYNAMO_TABLE_NAME
The partition key for the DynamoDB table should be set as connectionId
.
Configure the Execution role of Lambda functions as follows:
Grant the following permissions to enable access to the related services:
- AmazonAPIGatewayInvokeFullAccess
- AmazonDynamoDBFullAccess
- AWSXRayDaemonWriteAccess
- AWSLambdaBasicExecutionRole
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
To test the WebSocket API, you can use wscat, an open-source command line tool.
- Install NPM.
- Install wscat:
$ npm install -g wscat
- On the console, connect to your published API endpoint by executing the following command:
$ wscat -c wss://{YOUR-API-ID}.execute-api.{YOUR-REGION}.amazonaws.com/{STAGE}
- To test the sendMessage function, send a JSON message like the following example. The Lambda function sends it back using the callback URL:
$ 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