wppconnect-team / wppconnect

WPPConnect is an open source project developed by the JavaScript community with the aim of exporting functions from WhatsApp Web to the node, which can be used to support the creation of any interaction, such as customer service, media sending, intelligence recognition based on phrases artificial and many other things, use your imagination
https://wppconnect.io
Other
1.86k stars 328 forks source link

Missing messages after reopening an existing session #2346

Open manfe opened 1 week ago

manfe commented 1 week ago

Description

Some events are missing, looks like a race condition issue. I've tested with 1 business account, 1 personal account and another 2 numbers (these 2 numbers didn't triggered this issue). It's not clear when this happen, but it's happening!

Environment

Steps to Reproduce

create a regular session, my code:

create({
  session: "test",
  headless: false,
  devtools: true,
  statusFind: (statusSession, session) => {
    // return: isLogged || notLogged || browserClose || qrReadSuccess || qrReadFail || autocloseCalled || desconnectedMobile || deleteToken
    console.log("Status Session: ", statusSession);
    // create session wss return "serverClose" case server for close
    console.log("Session name: ", session);
  },
})
  .then((client) => start(client))
  .catch((error) => console.log(error));

function start(client: Whatsapp) {
  client.getWAVersion().then((version) => {
    console.log("WA Version: ", version);
  });

  client.onAnyMessage((message) => {
    console.log("New Message: ", message.body);
  });

  client.listChats().then((chats) => {
    console.log("chats: ", chats.length);
  });
}

After having a existing session and connected, terminate the process and send 15 messages and then run the process again to start receiving the messages while it was disconnected.

When I sent 9 messages, in this case, first 1-4 messages normally are lost.

In this case, messages with body 1, 2, 3 were lost

debug:    [manfe:client] Emitting onAnyMessage event (1 registered)
New Message:  4
debug:    [manfe:client] Emitting onAnyMessage event (1 registered)
New Message:  5
debug:    [manfe:client] Emitting onAnyMessage event (1 registered)
New Message:  6
debug:    [manfe:client] Emitting onAnyMessage event (1 registered)
New Message:  7
debug:    [manfe:client] Emitting onAnyMessage event (1 registered)
New Message:  8
debug:    [manfe:client] Emitting onAnyMessage event (1 registered)
New Message:  9

So I started debugging and I noticed that listener.layer.js is being injected/called after WA.js is already emiting some events for `u.MsgStore.on("add", (e) => {})

But if I add a console.log to the WA.js inside the function when the add is emitted I see the missing messages as isNewMsg.