API Gateway WebSocket API

  • For real-time and stateful application in API Gateway
  • Client connect to server
    • Connecting using wss://[unique-id].execute-api.[region].amazonaws.com/[stage-name]Pasted image 20221015141204.png
    • We invoke the onConnect lambda function upon connection and the lambda function can store connectionId to DynamoDB for example
  • Client to server
    • When client sending messages (frames) it invoke a sendMessage lambda function witht he same connectionId Pasted image 20221015141405.png
  • Server returns client
    • Server talk back to client using a Connection URL callback
      • Lambda function can then do a POST to the URL callback to connect back to the client
      • wss://[uniqueid].execute-api.[region].amazonaws.com/[stage]/@connections/[connectionId]Pasted image 20221015141926.png
  • Connection URL
    • Has the format wss://[uniqueid].execute-api.[region].amazonaws.com/[stage]/@connections/[connectionId]
      • POST: send message from Server to Client
      • GET: get the latest connection status of the connected WS Client
      • DELETE: Disconnect the client from WS connection

Routing

  • Incoming JSON messages are routed to different back end
  • If ther is no rount, it sends to $default
  • Specify a route selection to select the field on JSON to act as route
    • For example, $request.body.actionPasted image 20221015142832.png
    • The result is avaluated against route keys available in your API Gateway
    • The route then connected to backend you've setup through API gateway