API Gateway Chat

API Gateway creation Websocket

Announcing WebSocket APIs in Amazon API Gateway:

  1. Create a new WebSocket API

  2. In the API Gateway console, choose Create API, New API.

  3. Under Choose the protocol, choose WebSocket.

  4. For API name, enter My Chat API.

  5. For Route Selection Expression, enter $request.body.action.

  6. Enter a description if you’d like and click Create API.

Websocket: paths and stage

System paths

Connect:

Connect Nodejs script github

Integration Request Configuration

Provide information about the target backend that this route will call and whether the incoming payload should be modified.

Disconnect

Disonnect Nodejs script github

Default

Default Nodejs script github


Custom paths

Conversation

Conversation Nodejs script github


{

"action" : "conversation",

"text" : "hola"

}



When lambda need to execute API:

//ApiGatewayManagementApi makes a POST

// post https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/ApiGatewayManagementApi.html#postToConnection-property

apiGatewayManagementApi.postToConnection(paramsToSendToChatGroup, function(err, data) {

if (err) console.log(err, err.stack); // an error occurred

else console.log(data); // successful response

});


you need to give permissoin to lambda to execute ApiGatewayManagementApi and just work with Nodejs.12

JSON structure

API Gateway Chat Stage and deploy

Remember to DELPLOY api ws