在 python 中向特定客户端 connectionId Websocket API AWS 发送消息?

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

我正在尝试在 API 网关中使用 AWS Lambda + Websocket API 构建一个实时聊天应用程序。

我想做的是将每个用户的 connectionId 保存在我的数据库中,以便能够在 2 个用户处于同一对话中时向特定客户端发送消息。

如何从 python 中的 lambda 函数指定 connectionIds 目标?

这实际上应该是 connectionId 的列表,要发送给应用程序当前对话中与我交谈的每个人。

伪代码看起来像(在 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}

提前致谢

解决方案是使用 APIGatewayManagementAPI 并使用采用特定 ConnectionId 参数的 post_to_connection 方法将消息发送到特定客户端:

并且 here 是我在其中找到的 boto3 文档的 link。