Sending parameters to AWS API gateway websocket connect method

Viewed 2137

I followed this tutorial to create a small chat application using python, aws apigategay and serverless. When connecting to the Websocket the connection ID is stored in a Database.

Everything works as expected, now I want to be able to connect to the Websocket with a role, lets say for example the chat allows customers and store employees.

I'm trying to find a way to send a parameter to the connect method but no luck so far. For example when establishing a connection I want to send a role ID and store in the database to specify that aconnection ID belongs to a customers or to store employees.

I read about the API gateway authorization but the thing is that the chat will be included in an application that's already running that is not using AWS authorization.

Is there any way to accomplish this?

2 Answers

You can send parameters via query string as follow:

 $  wscat -c wss://ws.mycustomdomain.com/?test=123

Then you can get that param from event.queryStringParameters in the function handler.

def handler(event, context):
    connectionId = event['queryStringParameters']['test']

Hope that helps

Send the value as header in your connect call and retrieve it in backend to store alongside connectionId.

wscat --connect wss://aaaaaaaa.execute-api.us-east-2.amazonaws.com/test --header myHeader:myValue

In my lambda, the headers looked like,

"headers": {
        "Host": "xxxxxxx.execute-api.us-east-2.amazonaws.com",
        "myHeader": "myValue",
        "Sec-WebSocket-Extensions": "permessage-deflate; client_max_window_bits",
        "Sec-WebSocket-Key": "xxxxxxx",
        "Sec-WebSocket-Version": "13",
        "X-Amzn-Trace-Id": "xxxxxxx",
        "X-Forwarded-For": "xxxxxx",
        "X-Forwarded-Port": "443",
        "X-Forwarded-Proto": "https"
    },
Related