Skip to content

Commit a605d85

Browse files
committed
initial import
1 parent 15ef34b commit a605d85

File tree

12 files changed

+452
-5
lines changed

12 files changed

+452
-5
lines changed

CODE_OF_CONDUCT.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
## Code of Conduct
22
This project has adopted the [Amazon Open Source Code of Conduct](https://aws.github.io/code-of-conduct).
33
For more information see the [Code of Conduct FAQ](https://aws.github.io/code-of-conduct-faq) or contact
4-
[email protected] with any additional questions or comments.
4+
[email protected] with any additional questions or comments.

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,4 @@ If you discover a potential security issue in this project we ask that you notif
5858

5959
See the [LICENSE](https://github.com/aws-samples/simple-websockets-chat-app/blob/master/LICENSE) file for our project's licensing. We will ask you to confirm the licensing of your contribution.
6060

61-
We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes.
61+
We may ask you to sign a [Contributor License Agreement (CLA)](http://en.wikipedia.org/wiki/Contributor_License_Agreement) for larger changes.

Config

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# -*-perl-*-
2+
3+
package.ComputeBlogAPIGW-20 = {
4+
interfaces = (1.0);
5+
6+
deploy = {
7+
generic = true;
8+
};
9+
10+
build-environment = {
11+
chroot = basic;
12+
network-access = blocked;
13+
};
14+
15+
# Use NoOpBuild. See https://w.amazon.com/index.php/BrazilBuildSystem/NoOpBuild
16+
build-system = no-op;
17+
build-tools = {
18+
1.0 = {
19+
NoOpBuild = 1.0;
20+
};
21+
};
22+
23+
# Use runtime-dependencies for when you want to bring in additional
24+
# packages when deploying.
25+
# Use dependencies instead if you intend for these dependencies to
26+
# be exported to other packages that build against you.
27+
dependencies = {
28+
1.0 = {
29+
};
30+
};
31+
32+
runtime-dependencies = {
33+
1.0 = {
34+
};
35+
};
36+
37+
};

README.md

+36-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,40 @@
1-
## Simple Websockets Chat App
1+
# simple-websockets-chat-app
2+
3+
This is the template for simple-websocket-chat-app - Below is a brief explanation of what we have generated for you:
4+
5+
```
6+
.
7+
├── README.md <-- This instructions file
8+
├── onconnect <-- Source code onconnect excerpt
9+
├── ondisconnect <-- Source code ondisconnect excerpt
10+
├── sendmessage <-- Source code sendmessage excerpt
11+
└── template.yaml <-- SAM template with Lambda Functions and DDB
12+
```
13+
14+
15+
# Deploying on your account
16+
17+
## AWS CLI commands
18+
19+
AWS CLI commands to package, deploy and describe outputs defined within the cloudformation stack:
20+
21+
```
22+
sam package \
23+
--template-file template.yaml \
24+
--output-template-file packaged.yaml \
25+
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
26+
27+
sam deploy \
28+
--template-file packaged.yaml \
29+
--stack-name simple-websocket-chat-app \
30+
--capabilities CAPABILITY_IAM \
31+
--parameter-overrides MyParameterSample=MySampleValue
32+
33+
aws cloudformation describe-stacks \
34+
--stack-name simple-websocket-chat-app --query 'Stacks[].Outputs'
35+
```
236

3-
This SAM application provides the Lambda functions, DynamoDB table, and roles to allow you to build a simple chat application based on API Gateway's new WebSocket-based API feature.
437

538
## License Summary
639

7-
This sample code is made available under a modified MIT license. See the LICENSE file.
40+
This sample code is made available under a modified MIT license. See the LICENSE file.

onconnect/app.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
4+
var AWS = require("aws-sdk");
5+
AWS.config.update({ region: process.env.AWS_REGION });
6+
var ddb = new AWS.DynamoDB({ apiVersion: "2012-10-08" });
7+
8+
exports.handler = async (event, context, callback) => {
9+
var putParams = {
10+
TableName: "simplechat",
11+
Item: {
12+
connectionId: { S: event.requestContext.connectionId }
13+
}
14+
};
15+
16+
ddb.putParams(putParams, function(err, data) {
17+
callback(null, {
18+
statusCode: err ? 500 : 200,
19+
body: JSON.stringify(data);
20+
});
21+
});
22+
};

onconnect/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "onConnect",
3+
"version": "1.0.0",
4+
"description": "OnConnect example for WebSockets on API Gateway",
5+
"main": "src/app.js",
6+
"author": "SAM CLI",
7+
"license": "MIT"
8+
}

ondisconnect/app.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
4+
var AWS = require('aws-sdk');
5+
AWS.config.update({region: process.env.AWS_REGION});
6+
var ddb = new AWS.DynamoDB({apiVersion: "2012-10-08"});
7+
8+
exports.handler = function (event) {
9+
var scanParams = {
10+
TableName: "sipmlechat",
11+
ProjectionExpression: "connectionId"
12+
};
13+
14+
ddb.scan(scanParams, function (err, data) {
15+
if (err) {
16+
callback(null, {
17+
statusCode: 500,
18+
body: JSON.stringify(data)
19+
});
20+
} else {
21+
var apigwManagementApi = new AWS.ApiGatewayManagementApi({
22+
apiVersion: "2018-11-29",
23+
endpoint: event.context.apiId + ".execute-api." + process.env.AWS_REGION + ".amazonaws.com/" + event.context.stage
24+
});
25+
var body = JSON.parse(event.body);
26+
27+
data.Items.forEach(function (element) {
28+
var postParams = {
29+
connectionId: element.connectionId.S,
30+
data: body.message
31+
};
32+
33+
apigwManagementApi.postToConnection(postParams, function (err, data) {
34+
if (err) {
35+
// API Gateway returns a status of 410 GONE
36+
// when the connection is no longer available.
37+
// If this happens, we simply delete the
38+
//identifier from our DynamoDB table.
39+
if (err.statusCode === 410) {
40+
console.log("Found stale connection, deleting " + postParams.connectionId);
41+
DDB.deleteItem({ TableName: process.env.TABLE_NAME,
42+
Key: { connectionId: { S: postParams.connectionId } } });
43+
} else {
44+
console.log("Failed to post. Error: " + JSON.stringify(err));
45+
}
46+
}
47+
});
48+
});
49+
50+
callback(null, {
51+
statusCode: 200,
52+
body: "Sent."
53+
});
54+
}
55+
})
56+
};

ondisconnect/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "sendMessage",
3+
"version": "1.0.0",
4+
"description": "sendMessage example for WebSockets on API Gateway",
5+
"main": "src/app.js",
6+
"author": "SAM CLI",
7+
"license": "MIT"
8+
}

packaged.yaml

+107
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
AWSTemplateFormatVersion: '2010-09-09'
2+
Description: 'simple-websockets-chat-app
3+
4+
SAM Template for simple-websockets-chat-app that has the DynamoDB table and Lambda
5+
functions needed to demonstrate the Websocket protocol on API Gateway.
6+
7+
'
8+
Globals:
9+
Function:
10+
Environment:
11+
Variables:
12+
DDBTABLE: simplechat
13+
REGION: us-west-1
14+
Runtime: nodejs8.10
15+
Timeout: 180
16+
Outputs:
17+
OnConnectFunction:
18+
Description: OnConnect function ARN
19+
Value:
20+
Fn::GetAtt:
21+
- OnConnectFunction
22+
- Arn
23+
OnDisconnectFunction:
24+
Description: OnDisconnect function ARN
25+
Value:
26+
Fn::GetAtt:
27+
- OnDisconnectFunction
28+
- Arn
29+
SendMessageFunction:
30+
Description: SendMessage function ARN
31+
Value:
32+
Fn::GetAtt:
33+
- SendMessageFunction
34+
- Arn
35+
SimpleChatTable:
36+
Description: DynamoDB Table for simplechat ARN
37+
Value:
38+
Fn::GetAtt:
39+
- SimpleChatTable
40+
- Arn
41+
Resources:
42+
OnConnectFunction:
43+
Properties:
44+
CodeUri: s3://wss-over-apigw/901d38992aff682fcd3525398ecc3c77
45+
Handler: app.handler
46+
MemorySize: 256
47+
Policies:
48+
- Statement:
49+
- Action:
50+
- DynamoDB:*
51+
Effect: Allow
52+
Resource:
53+
- arn:aws:dynamodb:*:*:table/simplechat
54+
Runtime: nodejs8.10
55+
Type: AWS::Serverless::Function
56+
OnDisconnectFunction:
57+
Properties:
58+
CodeUri: s3://wss-over-apigw/f9d95258b784304e4acbcfe6c19d7da8
59+
Handler: app.handler
60+
MemorySize: 256
61+
Policies:
62+
- Statement:
63+
- Action:
64+
- DynamoDB:*
65+
Effect: Allow
66+
Resource:
67+
- arn:aws:dynamodb:*:*:table/simplechat
68+
Runtime: nodejs8.10
69+
Type: AWS::Serverless::Function
70+
SendMessageFunction:
71+
Properties:
72+
CodeUri: s3://wss-over-apigw/a4eeb8492b43bb64e4a9363e497a372d
73+
Environment:
74+
Variables:
75+
ACCESSKEY: samplekey
76+
HOST: wss://samplehost.execute-api.us-west-1.amazonaws.com
77+
SECRET: samplesecret
78+
Handler: app.handler
79+
MemorySize: 256
80+
Policies:
81+
- Statement:
82+
- Action:
83+
- DynamoDB:*
84+
Effect: Allow
85+
Resource:
86+
- arn:aws:dynamodb:*:*:table/simplechat
87+
- Statement:
88+
- Action:
89+
- execute-api:ManageConnections
90+
Effect: Allow
91+
Resource:
92+
- arn:aws:execute-api:*:*:*/*/@connections/*
93+
Runtime: nodejs8.10
94+
Type: AWS::Serverless::Function
95+
SimpleChatTable:
96+
Properties:
97+
PrimaryKey:
98+
Name: connectionId
99+
Type: String
100+
ProvisionedThroughput:
101+
ReadCapacityUnits: 2
102+
WriteCapacityUnits: 2
103+
SSESpecification:
104+
SSEEnabled: true
105+
TableName: simplechat
106+
Type: AWS::Serverless::SimpleTable
107+
Transform: AWS::Serverless-2016-10-31

sendmessage/app.js

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
// Copyright 2018 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2+
// SPDX-License-Identifier: MIT-0
3+
4+
var AWS = require('aws-sdk');
5+
AWS.config.update({region: process.env.AWS_REGION});
6+
var ddb = new AWS.DynamoDB({apiVersion: "2012-10-08"});
7+
8+
exports.handler = function (event) {
9+
var scanParams = {
10+
TableName: "sipmlechat",
11+
ProjectionExpression: "connectionId"
12+
};
13+
14+
ddb.scan(scanParams, function (err, data) {
15+
if (err) {
16+
callback(null, {
17+
statusCode: 500,
18+
body: JSON.stringify(data)
19+
});
20+
} else {
21+
var apigwManagementApi = new AWS.ApiGatewayManagementApi({
22+
apiVersion: "2018-11-29",
23+
endpoint: event.context.apiId + ".execute-api." + process.env.AWS_REGION + ".amazonaws.com/" + event.context.stage
24+
});
25+
var body = JSON.parse(event.body);
26+
27+
data.Items.forEach(function (element) {
28+
var postParams = {
29+
connectionId: element.connectionId.S,
30+
data: body.message
31+
};
32+
33+
apigwManagementApi.postToConnection(postParams, function (err, data) {
34+
if (err) {
35+
// API Gateway returns a status of 410 GONE
36+
// when the connection is no longer available.
37+
// If this happens, we simply delete the
38+
// identifier from our DynamoDB table.
39+
if (err.statusCode === 410) {
40+
console.log("Found stale connection, deleting " + postParams.connectionId);
41+
DDB.deleteItem({ TableName: process.env.TABLE_NAME,
42+
Key: { connectionId: { S: postParams.connectionId } } });
43+
} else {
44+
console.log("Failed to post. Error: " + JSON.stringify(err));
45+
}
46+
}
47+
});
48+
});
49+
50+
callback(null, {
51+
statusCode: 200,
52+
body: "Sent."
53+
});
54+
}
55+
})
56+
};

sendmessage/package.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"name": "sendMessage",
3+
"version": "1.0.0",
4+
"description": "sendMessage example for WebSockets on API Gateway",
5+
"main": "src/app.js",
6+
"author": "SAM CLI",
7+
"license": "MIT"
8+
}

0 commit comments

Comments
 (0)