WebSocket-based APIs are bidirectional in nature. This means that a client can send messages to a service and services can independently send messages to its clients.
This bidirectional behavior allows for richer types of client/service interactions because services can push data to clients without a client needing to make an explicit request.
WebSocket APIs are often used in real-time applications such as chat applications, collaboration platforms, multiplayer games, and financial trading platforms.
There are three special routeKey values that API Gateway allows you to use for a route:
$default
: Used when the route selection expression produces a value that does not match any of the other route keys in your API routes. This can be used, for example, to implement a generic error handling mechanism.$connect
: The associated route is used when a client first connects to your WebSocket API.$disconnect
: The associated route is used when a client disconnects from your API. This call is made on a best-effort basis.
In the API Gateway console, create a new api:
-
select WebSocket
-
API name:
wsEcho
-
Route selection expression:
$request.body.action
-
Description:
echo websocket information
-
Add new route key
echo
to handle$request.body.action
We will park here. Create lambda functions to handle different routes.
-
In IAM console, click on create role.
-
Select
Lambda
service -
Policies:
- AWSLambdaBasicExecutionRole
- AWSLambdaVPCAccessExecutionRole
- AmazonAPIGatewayInvokeFullAccess
-
service: api gateway
-
policy: AmazonAPIGatewayPushToCloudWatchLogs
-
copy the arn and put in the api gateway setting to enable cloudwatch log.
-
Create a cloud9 environment
-
click on AWS Resources and then create a local lambda function
-
Setup the Applicaiton and Lambda Function
- Application name:
webSocketsOnAPIGW
- Function name:
wsEcho
- runtime:
node.js 6.10
- blueprint
empty-nodejs
- Function trigger:
none
- Role: select existing role:
lambda_basic_execution_role
- Application name:
-
In the right panel, right click on
webSocketsOnAPIGW
and selectCreate Here
:- Function name:
wsConnect
,wsDisconnect
, andwsDefault
- runtime:
node.js 6.10
- blueprint
empty-nodejs
- Function trigger:
none
- Role: select existing role:
lambda_basic_execution_role
- Function name:
-
Modify template.yaml
-
All runtime: nodejs8.10
-
change hander name:
- Handler: wsEcho/echo.handler
- Handler: wsConnect/connect.handler
- Handler: wsDisconnect/disconnect.handler
- Handler: wsDefault/default.handler
-
You can reference template.yaml sample
-
-
Rename index.js file name to the meaningful file names:
- wsEcho/echo.js
- wsConnect/connect.js
- wsDisconnect/disconnect.js
- wsDefault/default.js
-
Copy the source code to related files:
-
Create a package.json:
-
In the cloud9, open a new terminal. Go to wsEcho to install related package.
-
In lambda console, you will see the functions. If error, please check the cloudformation for detail information.
-
Click on the
Integration Request
for theecho
routes. Then bind to echo lambda function. -
follow the same process to bind
$connect
,$disconnect
, and$default
. -
Click on the
Actions
then deploy api to stagedev
-
Copy the web socket end points. Setting Logs/Tracing.
-
Then, in the cosonle, use the command
wscat -c wss://<endpoint-id>.execute-api.us-east-1.amazonaws.com/dev
-
Check cloud watch for api and lamdba.