Spankied
Spankied

Reputation: 1885

AWS Websocket doesnt receive previous message until new message is sent

Most of the time the messages are passed normally, but a couple messages in particular arent recieved until the recieving client sends a message. This happens everytime for specific methods/messages, but not at all for others.

Example: user1 sends a message, user2 then sends a message to receive message from user1.


Related Material

  1. Deleted question: websocket receives previous message only when new message is sent
  2. Github issue: webSocket client does not receive messages before sending...

Upvotes: 3

Views: 982

Answers (1)

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
}

Upvotes: 2

Related Questions