aws-sdk#ApiGatewayManagementApi TypeScript Examples

The following examples show how to use aws-sdk#ApiGatewayManagementApi. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: message.ts    From flect-chime-sdk-demo with Apache License 2.0 4 votes vote down vote up
/**
 * message
 * (1) Gather the information of attendees in the same meeting
 * (2) get endpoint of API GW
 * (3) decide the destination
 * (4) send message
 * @param {*} event
 * @param {*} context
 * @param {*} callback
 */
exports.message = async (event: any, context: any, callback: any) => {
    console.log(event);
    console.log(context);
    console.log(callback);
    console.log("sendmessage event:", JSON.stringify(event, null, 2));
    console.log("meetingId", event.requestContext.authorizer.meetingId);
    //// (1) Gather the information of attendees in the same meeting
    let attendees;
    try {
        attendees = await ddb
            .query({
                ExpressionAttributeValues: {
                    ":meetingId": { S: event.requestContext.authorizer.meetingId },
                },
                KeyConditionExpression: "MeetingId = :meetingId",
                ProjectionExpression: "ConnectionId, AttendeeId",
                TableName: process.env.CONNECTION_TABLE_NAME!,
            })
            .promise();
    } catch (e) {
        console.log("Query error:", e);
        return { statusCode: 500, body: JSON.stringify(e) };
    }

    //// (2) get endpoint of API GW
    const apigwManagementApi = new ApiGatewayManagementApi({
        apiVersion: "2018-11-29",
        endpoint: `${event.requestContext.domainName}/${event.requestContext.stage}`,
    });

    //// (3) decide the destination
    console.log("DATA:", event.body);
    console.log("Attendee!!!:", attendees);
    const body = JSON.parse(event.body);
    const targetId = body.targetId;
    const privateMessage = body.private;

    console.log("targetId:", targetId);
    console.log("private:", privateMessage);

    /// (4) send message
    const postCalls = attendees.Items!.map(async (connection: any) => {
        const connectionId = connection.ConnectionId.S;
        const attendeeId = connection.AttendeeId.S;
        if (privateMessage !== true || attendeeId === targetId) {
            try {
                const res = await apigwManagementApi
                    .postToConnection({
                        ConnectionId: connectionId,
                        Data: JSON.stringify(body),
                    })
                    .promise();
                console.log("done sending!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1", res);
                console.log("done sending!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!2", connectionId);
                console.log("done sending!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!3", JSON.stringify(body));
            } catch (e) {
                console.error(`error posting to connection ${connectionId}: ${JSON.stringify(e)}`);
            }
        }
    });

    try {
        const res = await Promise.all(postCalls);
        console.log("RESPONSE!", res);
    } catch (e) {
        console.error(`failed to post: ${JSON.stringify(e)}`);
        return { statusCode: 500, body: JSON.stringify(e) };
    }

    body.done = true;
    // body.content.fileParts = "" // reduce trafic

    return { statusCode: 200, body: JSON.stringify(body) };
};