Issue Description
WhatsApp Client Events Firing Multiple Times
Description
WhatsApp client events are firing multiple times instead of once. The authenticated, ready, and message events trigger repeatedly for a single occurrence.
Expected Behavior
authenticated event should fire once per successful login
message event should fire once per incoming message
Actual Behavior
authenticated event fires 5 times for a single login
message event fires multiple times per message
Logs
Registering even handlers
[INFO] Whatsapp login status: { isReady: false, hasQRCode: true, qrCodePresent: true }
[INFO] WhatsApp client authenticated successfully
[INFO] WhatsApp client authenticated successfully
[INFO] WhatsApp client authenticated successfully
[INFO] WhatsApp client is now READY - Can send/receive messages!
Message part happen some time, when so many message come, as much as I observed, not every time.
Impact
- Business logic runs multiple times unnecessarily
- Increased CPU/memory usage
- Unpredictable behavior in production
If this is a mistake from my side, please let me know. I am sorry for that.
Reproduction Steps
Steps To Reproduce
- Initialize the WhatsApp client
- Register all event listeners (
authenticated, ready, message)
- Start the client and scan the QR code
- Wait for authentication and ready state
- Send a message to the connected WhatsApp number
- Observe that the events are triggered multiple times instead of once
Code Sample
const WHATSAPP_CONSTANTS = {
COUNTRY_CODE: '91',
INDIAN_PHONE_LENGTH: 10,
FULL_PHONE_LENGTH: 12,
SESSION_PATH: './whatsapp_session'
};
let whatsappClient = null;
let isClientReady = false;
let currentQRCode = null;
let currentQRCodeBase64 = null;
let isInitializing = false;
function createWhatsAppClient() {
const sessionPath = WHATSAPP_CONSTANTS.SESSION_PATH;
const clientConfig = {
authStrategy: new LocalAuth({
dataPath: sessionPath
}),
puppeteer: {
headless: true,
args: [
'--no-sandbox',
'--disable-setuid-sandbox',
'--disable-dev-shm-usage',
'--disable-accelerated-2d-canvas',
'--no-first-run',
'--no-zygote',
// '--single-process',
'--disable-gpu',
'--disable-extensions'
]
}
};
const client = new Client(clientConfig);
whatsappClient = client;
return true;
}
function setupWhatsAppEventHandlers() {
console.log("Registering even handlers")
whatsappClient.on('qr', async (qr) => {
await handleQRCodeGeneration(qr);
});
whatsappClient.on('authenticated', () => {
logger.info('WhatsApp client authenticated successfully');
});
whatsappClient.on('ready', () => {
handleClientReady();
});
whatsappClient.on('disconnected', (reason) => {
handleClientDisconnected(reason).catch(error => {
logger.error('Error handling client disconnect:', error);
});
});
whatsappClient.on('auth_failure', (message) => {
handleAuthenticationFailure(message);
});
whatsappClient.on('message', (message) => {
handleIncomingMessage(message).catch(error => {
logger.error('Error handling incoming message:', error);
});
});
whatsappClient.on('call', async (call) => {
handleIncomingCall(call).catch(error => {
logger.error('Error handling incoming call:', error);
});
});
whatsappClient.on('error', (error) => {
logger.error('WhatsApp client error:', error);
});
whatsappClient.on('change_state', (state) => {
logger.info('Whatsapp state change:', state);
});
return true;
}
async function initializeWhatsAppService() {
if (whatsappClient) {
logger.warn('WhatsApp client already exists, skipping initialization');
return false;
}
if (isInitializing) {
logger.warn('WhatsApp already initializing, skipping duplicate call');
return false;
}
isInitializing = true;
try {
const clientCreated = createWhatsAppClient();
const handlersSetup = setupWhatsAppEventHandlers();
if (clientCreated && handlersSetup) {
await whatsappClient.initialize();
return true;
}
return false;
} catch (err) {
logger.error('initializeWhatsAppService error:', err.message);
return false;
} finally {
isInitializing = false;
}
}
User Setup
| WhatsApp |
Type |
| Account Type |
Standard |
| Authentication Strategy |
LocalAuth |
| WhatsApp Web Version |
2.3000.1039873748 |
| whatsapp-web.js Version |
^1.34.6 |
| Environment |
Version |
| Browser Type |
Chrome |
| Browser Version |
146.0.7680.31 |
| Phone OS Version |
Android 10 |
| Running OS Version |
Windows 11 |
| Node.js Version |
22.13.0 |
Checklist
Issue Description
WhatsApp Client Events Firing Multiple Times
Description
WhatsApp client events are firing multiple times instead of once. The
authenticated,ready, andmessageevents trigger repeatedly for a single occurrence.Expected Behavior
authenticatedevent should fire once per successful loginmessageevent should fire once per incoming messageActual Behavior
authenticatedevent fires 5 times for a single loginmessageevent fires multiple times per messageLogs
Message part happen some time, when so many message come, as much as I observed, not every time.
Impact
If this is a mistake from my side, please let me know. I am sorry for that.
Reproduction Steps
Steps To Reproduce
authenticated,ready,message)Code Sample
User Setup
Checklist