I manage a service that utilizes whatsapp-web.js to send messages to large (more than 400 participants) groups. This service operates on an Ubuntu Linux server and is implemented using a Node.js script.
When the script sends a few messages per minute, it typically begins to experience significant delays in message delivery—ranging from 15 seconds to several minutes, depending on the number of messages. In some instances, the script stops sending messages altogether and does not recover until approximately 30 minutes have passed. Restarting the server does not resolve the issue. This issue also happens when forwarding messages as well. Monitoring the service with PM2 revealed that the script itself does not consume many resources, but the Chrome instance running in headless mode via Puppeteer does.
To troubleshoot, I began debugging by sending the same messages to a single-person chat, where they were delivered instantly. This led me to conclude that the size of the group is the primary factor affecting message delivery performance.
I attempted to open a case on GitHub regarding this issue, but it was unfortunately closed as “not planned.”
After extensive investigation, I believe I have identified the root cause of the problem: the chat itself was exceptionally large. Although it contained no images or other media, it had accumulated thousands of messages over time. Loading such a vast chat in the web UI likely caused the system to become unresponsive, leading to the delays and interruptions in message delivery.
So, the solution for me was to clear all the chats that are a group in a function, and after running it everything went back to being instantaneous:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
async function clearAllChats() { try { // Get all chats const chats = await client.getChats(); // Iterate through each chat for (let chat of chats) { // Check if the chat is a group if (chat.isGroup) { writeToLogFile(`Clearing chat for group: ${chat.name}`); // Use clearChat function to clear the chat try { await chat.clearMessages(); writeToLogFile(`Chat cleared for group: ${chat.name}`); } catch (err) { writeToLogFile(`Error clearing chat for group: ${chat.name}` + err); } } } } catch (error) { console.error('An error occurred:', error); } } |
Also, I found that sending to group chats like this provides the best performance:
1 2 3 4 5 6 |
const options = { sendSeen: false, linkPreview: false, parseVCards: false, }; await client.sendMessage('xxxxxxxxx@c.us' , message, options); |
How to use?
As a message trigger:
1 2 3 4 5 6 7 8 9 |
client.on('message', async message => { console.log(message.from + ' ' + message.to); writeToLogFile('Received message from ' + message.from ); //clearAllChats if(message.body.toLowerCase() === 'clear') { await clearAllChats(); await message.reply('cleared.'); } }); |
On client ready:
1 2 3 4 |
client.on('ready', async () => { console.log('Client is ready!'); await clearAllChats(); }); |
Cloud solutions
If you’re looking for a reliable cloud solution and want to support my work at the same time, DigitalOcean is the perfect choice. With my affiliate link, you’ll receive $200 worth of free hosting products, giving you the chance to explore their powerful and easy-to-use platform at no cost. Whether you’re a beginner or an experienced developer, DigitalOcean offers everything you need to build, scale, and manage your website or app with ease. By signing up through my link, you not only help me but also get an incredible deal to start your cloud journey. Don’t miss this opportunity—get started with DigitalOcean today!
Click on the affiliate link below to get your free 200$ starter budget.