Search code examples
javaamazon-web-serviceswebsocketaws-api-gatewayreal-time

Java object received in AWS API gateway websocket api on connect


I need to store the connection Id in a database and hence on the $connect call to the API would like to receive the payload containing the connection id and message in the Spring boot Java Controller in HttpProxy created against $connect in API gateway WebSocket Protocol.

WebSocket URL: wss://xxxx.execute-api.us-west-2.amazonaws.com/test

HttpProxy: https://xxx.xxxxxx.com/create-connection
I decided to do a POST call with the payload as the Request body to the HttpProxy.

Also set the Request parameters using the AWS CLI:

"RequestParameters": {
    "integration.request.header.connectionId": "context.connectionId"
}

But still not getting the connectionId in the parameters


Solution

  • A workaround maybe but for sending the connection id, request headers solved the situation for me here. We can map the connection id from the Integration request to the request header of the Http Proxy.
    Also this configuration is not yet supported from the AWS Console rather we need to use the aws-cli to configure this.
    Refering documentation,

    aws apigatewayv2 update-integration \
        --integration-id abc123 \
        --api-id a1b2c3d4 \ 
        --request-parameters 'integration.request.header.connectionId'='context.connectionId'
    

    You can also forward other connection details, refer to the documentation.

    You can also send parameters to the websocket URL which will be carry forwarded to the Http Proxy as it is.
    If you are using wscat to connect to the websocket url from the terminal running ZSH, then it will not recognize the url with parameters. You will need to put the whole url in double quotes.

    ➜  ~ wscat -c "wss://xxxx.execute-api.<region>.amazonaws.com/<stage-name>?param=1&param2=2"