AWS web socket api won't respond to action

258 views Asked by At

This code should respond to allowAccess and declineAccess events by sending connectionId and message to the user who sent the request(CLI).

const AWS = require('aws-sdk');

const ENDPOINT = '0ai42l08l3.execute-api.us-east-1.amazonaws.com/production/';

const client = new AWS.ApiGatewayManagementApi({
    endpoint: ENDPOINT
});

async function sendToRequestor(id, message) {
    try {
        await client.postToConnection({
            'ConnectionId': id,
            'Data': Buffer.from(JSON.stringify(message))
        });
    } catch(err) {
        console.log(err);
    }
}

exports.handler = async (event) => {
    const connectionId = event.requestContext.connectionId;
    const route = event.requestContext.routeKey;
    
    switch(route) {
        case '$default':
            break;
        case '$connect':
            break;
        case '$disconnect':
            break;
        case 'createRoom':
            break;
        case 'allowAccess':
            await sendToRequestor(connectionId, 'accessAllowed');
            break;
        case 'declineAccess':
            await sendToRequestor(connectionId, 'accessDeclined');
            break;
        default:
            console.log('Unknown route');
            break;
    }
    
    const response = {
        statusCode: 200,
        body: JSON.stringify(event),
    };
    return response;
};

But when I send { "action": "allowAccess" } or { "action": "declineAccess" } using wscat I don't get any response from the server

0

There are 0 answers