async function sendData()

in backend/sendchatlayer/index.js [33:64]


async function sendData(msgcontent, connectionData, domain, tablename, stage) {
  delete msgcontent.domainName;
  delete msgcontent.stage;
  delete msgcontent.message.domainName;
  delete msgcontent.message.stage;
  const props = {
    apiVersion: '2018-11-29',
    endpoint: `${domain}/${stage}`,
  };
  const apigwManagementApi = new AWS.ApiGatewayManagementApi( props );

  const postCalls = connectionData.Items.map(async ({ connectionId }) => {
    try {
      await apigwManagementApi.postToConnection(
        { ConnectionId: connectionId, Data: JSON.stringify(msgcontent) },
      ).promise();
    } catch (e) {
      if (e.statusCode !== 410) {
        console.error(`Error with websocket connections ${JSON.stringify(e)} using ${JSON.stringify(props)}`);
        return { statusCode: 500, body: 'error with websocket connections' };
      }
    }
    return { statusCode: 200, body: 'all sent' };
  });
  try {
    await Promise.all(postCalls);
  } catch (e) {
    console.error(`Error with promises and table ${tablename}: ${JSON.stringify(e)}`);
    return { statusCode: 500, body: 'Error awaiting promises' };
  }
  return { statusCode: 200, body: 'messages sent' };
}