AWS Websocket doesnt receive previous message until new message is sent

Viewed 239
1 Answers

We ran into this issue and the solution had to do with how we wrote our promises. We initially used the sample code provided by Amazon

https://github.com/aws-samples/simple-websockets-chat-app/blob/master/sendmessage/app.js#L26

const postCalls = connectionData.Items.map(async ({ connectionId }) => {
    try {
      await apigwManagementApi.postToConnection({ ConnectionId: connectionId, Data: postData }).promise();
    } catch (e) {
      if (e.statusCode === 410) {
        console.log(`Found stale connection, deleting ${connectionId}`);
        await ddb.delete({ TableName: TABLE_NAME, Key: { connectionId } }).promise();
      } else {
        throw e;
      }
    }
  });

And I'm pretty sure having an async function as a map function doesn't work properly or reliably (for whatever reason. maybe this is documented somewhere), so we changed it to a simple for loop and it fixed the issue.

for(const connection of connectionData.Items) {
    const connectionId = connection.connectionId;
    ...same logic goes here
}
Related