AWS Deep Dive
- author:: Nathan Acks
- date:: 2022-11-24
Amazon API Gateway
Continued notes about the Amazon API Gateway.
Working With WebSocket APIs
Invoking WebSockets Backend Integrations in API Gateway
WebSocket APIs in API Gateway support messages up to 128 KB is size, chunked into blocks of at most 32 KB. Raw binary data is not supported.
Routes need to be configured before integrations for WebSocket APIs in the API Gateway console (this is less flexible than HTTP or REST APIs), and multiple routes cannot be associated with a single integration. These are both limitations of the API Gateway console, however, as using something like the AWS CLI that talks to the API Gateway API directly allows routes and integrations to be set up in any order, and for multiple routes to be paired with a single integration.
In addition to using data from request bodies for routing, API Gateway can also perform body validation before passing the request off to the backend. Unlike HTTP and REST APIs, explicit response routes need to be defined for WebSocket APIs to enable communication from the backend to the client.
Routes are defined by looking at the value of a specific JSON property; messages without that property (including non-JSON messages), or where that property does not match a defined route, are sent to the
WebSocket Selection Expressions in API Gateway
Route keys can actually include variables that refer to authentication headers, or the outcome of Lambda authorizers; however, everything else still needs to come from the incoming JSON body.
As of this writing, all route responses are handled in the same way (by a defined
$default response route). The API Gateway documentation indicates that this will eventually change, however.