I have a Node.js application that acts as a connector, facilitating message exchange between WhatsApp and another application, let's call it CCM. Here's how it works:
However, I'm encountering an issue: when multiple messages are sent in quick succession (2-3 messages together), CCM starts multiple chats for the same customer. This happens because CCM takes some time to process the first message, and meanwhile, the subsequent messages arrive quickly. Processing messages contains finding customer from db against the information contains in that message etc.
Questions:
It seems your issue is that the CCM processes multiple messages in parallel that are associated with the same customer.
- Is this behavior normal for the pub-sub system?
Yes. SNS sends messages to its subscribers and does not wait for confirmation that the message has been processed.
So, parallel message processing by the CCM can occur.
- Can anyone suggest solution of this problem?
- Shouldn't CCM wait for the acknowledgment of the first message before processing subsequent ones?
To address this, you can implement the following solution:
As a result, the CCM will not process a message for a particular customer if another or same CCM instance is already processing a message with the same MessageGroupId (i.e., customer ID). This guarantees that messages for the same customer are processed sequentially.