Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Send message to specific client connectionId Websocket API AWS in python?

I am trying to build a real-time chat app using AWS Lambda + Websocket API in API Gateway.

What I want to do is save the connectionId of each user in my DB to be able to send messages to specific clients when 2 users are in the same conversation for example.

How can I specify the connectionIds target from the lambda function in python?

This should actually be a list of connectionId to send to every people i am speaking with inside the app's current conversation.

The pseudo-code would look like (inside the lambda) :


def create_message(message, conversation):
    save_message_in_db(message)
    ids_that_should_receive_message = conversation["participantsID"]
    return {"body": json.dumps({"message": message}), "connectionId": ids_that_should_receive_message}

Thanks in advance

like image 720
Tom3652 Avatar asked Feb 01 '26 10:02

Tom3652


1 Answers

The solution is to use the APIGatewayManagementAPI and use the post_to_connection method that takes a specific ConnectionId parameter to send the message to a specific client :

post_to_connection

And here is the link to the boto3 documentation where i have found it.

like image 110
nicover Avatar answered Feb 03 '26 22:02

nicover