Skip to content

Commit f52a793

Browse files
committed
Add README.md to Python API Gateway WebSocket example
1 parent cf577a2 commit f52a793

File tree

1 file changed

+73
-0
lines changed
  • python/example_code/apigateway/websocket

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
# AWS API Gateway WebSocket Chat Program
2+
3+
## Repository files
4+
5+
* `websocket.py` : Main program source file
6+
* `lambda_util.py` : Utility functions to manage AWS Lambda functions
7+
* `websocket_connect.py` : AWS Lambda function to implement the WebSocket `$connect` route
8+
* `websocket_disconnect.py` : AWS Lambda function to implement the WebSocket `$disconnect` route
9+
* `websocket_send_msg.py` : AWS Lambda function to implement the WebSocket `sendmsg` custom route (Python)
10+
* `websocket_send_msg.js` : AWS Lambda function to implement the WebSocket `sendmsg` custom route (JavaScript)
11+
12+
## AWS infrastructure resources
13+
14+
* API Gateway WebSocket API
15+
* AWS Lambda functions for the WebSocket `$connect` and `$disconnect` routes and a WebSocket `sendmsg` custom route
16+
* AWS Identity and Access Management (IAM) role and policy for the AWS Lambda functions
17+
* Amazon DynamoDB table to store connection IDs and user names
18+
19+
## Prerequisites
20+
21+
* Install Python 3.x.
22+
* Install the AWS SDK for Python `boto3`. Instructions are at https://github.com/boto/boto3.
23+
* Install the AWS CLI (Command Line Interface). Instructions are at
24+
https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html.
25+
* Configure the AWS CLI. Instructions are at
26+
https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-getting-started.html.
27+
28+
## Instructions
29+
30+
To create the WebSocket infrastructure:
31+
32+
python websocket.py
33+
34+
To delete the WebSocket infrastructure:
35+
36+
python websocket.py -d
37+
OR
38+
python websocket.py --delete
39+
40+
To use the WebSocket Chat Program:
41+
42+
1. Download and install `Node.js` from https://nodejs.org. `Node.js` includes the `npm` package manager.
43+
2. Use `npm` to globally install `wscat`.
44+
45+
npm install -g wscat
46+
47+
3. When the WebSocket infrastructure is created, the WebSocket WSS address is output.
48+
Copy the output WebSocket WSS address and enter it on the `wscat` command line to open
49+
a WebSocket connection. Open multiple connections by running `wscat` in separate
50+
terminal windows.
51+
52+
wscat -c WSS_ADDRESS
53+
54+
Example command line:
55+
56+
wscat -c wss://123abc456def.execute-api.us-west-2.amazonaws.com/dev
57+
58+
Optional: Specify a user name as a query parameter in the WSS address (Used
59+
by the Python version of `websocket_send_msg.py`)
60+
61+
wscat -c wss://123abc456def.execute-api.us-west-2.amazonaws.com/dev?name=Steven
62+
63+
4. Send a chat message to all open connections:
64+
65+
{"action": "sendmsg", "msg": "Enter message text here"}
66+
67+
The `"action": "sendmsg"` pair invokes the WebSocket `sendmsg` custom route.
68+
69+
The `"msg": "Enter message text here"` pair specifies the message text to send.
70+
71+
5. To close the WebSocket connection:
72+
73+
<Ctrl-C>

0 commit comments

Comments
 (0)