pedroslopez / whatsapp-web.js

A WhatsApp client library for NodeJS that connects through the WhatsApp Web browser app
https://wwebjs.dev
Apache License 2.0
15.13k stars 3.61k forks source link

Gets the loaded messages sync progress percentage #2197

Closed MatanMaimon closed 9 months ago

MatanMaimon commented 1 year ago

Is there an existing issue for this?

Is your feature request related to a problem? Please describe.

I need to know when the client could use "whatsapp-web.js" after all his messages loaded

Describe the solution you'd like.

when user is first time scan the QR code, the client logged in (authentcated). however, this is not certain that all the messages (and contacts/other daa) are loaded to the whatsapp web client.

in the official Whatsapp Web - there is a progress bar indicates this.

also, the first loading screen of the chats (not ther data) is also something can be used.

the chats loading: load 1

the messages (and contacts i believe) loading: load2 1 load2 2

Describe an alternate solution.

No response

Additional context

No response

tofers commented 1 year ago
client.on('loading_screen', (percent, message) => {
    console.log('LOADING SCREEN', percent, message);
});
themazim commented 1 year ago

also, your client is ready to use when read event is triggered:

client.on('ready', () => {
    console.log('Client ready');
});
MatanMaimon commented 1 year ago
client.on('loading_screen', (percent, message) => {
    console.log('LOADING SCREEN', percent, message);
});

thanks, didn't knew of the (percent, message). so the "chats loading" seems to be fine with this solution.

however, right after the chats is 100% (~1-2 seconds) -> i can see when i use "someContact.fetchMessages(..)" i get only a few messages, and if i wait some time and do it again i get more messages...

so what i;m saying is that the "messages" syncing progress are not realy depends on the "loading_screen" event..

also, your client is ready to use when read event is triggered:

client.on('ready', () => {
    console.log('Client ready');
});

as i see this event is relevant only for the "cahts loading" and not the messages fetch sync (as described to @tofers

tofers commented 1 year ago

Yes it is just the loading of chats, the loading of messages will be synchronized when you receive messages in each chat. Otherwise the memory will quickly become clogged

I do not understand why this event is not in the documentation, it seems that something was missed

MatanMaimon commented 1 year ago

the loading of messages will be synchronized when you receive messages in each chat.

Not sure I understood what you meant by "the loading of messages will be synchronized when you receive messages in each chat."...

Is there a way to tell (with some event) if all old messages have synced across all my chats? Or is it really an additional feature that should be included?

The "loading_screen" feature only solves a part (the least important part, actually) of what I need

themazim commented 1 year ago

as far as I am concerned, there is no event for "chat messages loaded" for a specific chat / all chats.

What is your usecase?

In theory you probably can force chats being loaded by using fetchMessages method with an infinite (huge) limit.

MatanMaimon commented 1 year ago

as far as I am concerned, there is no event for "chat messages loaded" for a specific chat / all chats.

What is your usecase?

In theory you probably can force chats being loaded by using fetchMessages method with an infinite (huge) limit.

I am creating an application whose algorithm relies on the processing of the messages in the user's chats..

I need to know when the algorithm can really run on all the messages and not on a small part so that the information is correct

themazim commented 1 year ago

I think there are some issues regarding this:

  1. Old messages are not synced into whatsapp web / other whatsapp apps if the instances were just created. For example, I got my whatsapp iOS App as main, using it for 12 months and accumulated thousands of messages. Whatsapp web instance was started 10 days ago. I am not able to see older messages. I get the info "use whatsapp on your phone to view messages previous to a date 3 months ago".

  2. Whatsapp web is not loading all messages of a chat initially & automatically, there is some sort of "pagination" going on. if you scroll upwards, messages will be loaded (if they are present in the local database)

So, if you want to use all the messages that are available in the chat, I would suggest you use https://docs.wwebjs.dev/Chat.html#fetchMessages with limit 99999999 (or similar) as soon as the client is ready. Keep track of the message Ids and additionally listen to the message event to get new messages.

It is certainly possible to add an event if the currently available messages are loaded in the chat, but this wont solve the issues mentioned above.

MatanMaimon commented 1 year ago

I think there are some issues regarding this:

  1. Old messages are not synced into whatsapp web / other whatsapp apps if the instances were just created. For example, I got my whatsapp iOS App as main, using it for 12 months and accumulated thousands of messages. Whatsapp web instance was started 10 days ago. I am not able to see older messages. I get the info "use whatsapp on your phone to view messages previous to a date 3 months ago".
  2. Whatsapp web is not loading all messages of a chat initially & automatically, there is some sort of "pagination" going on. if you scroll upwards, messages will be loaded (if they are present in the local database)

So, if you want to use all the messages that are available in the chat, I would suggest you use https://docs.wwebjs.dev/Chat.html#fetchMessages with limit 99999999 (or similar) as soon as the client is ready. Keep track of the message Ids and additionally listen to the message event to get new messages.

It is certainly possible to add an event if the currently available messages are loaded in the chat, but this wont solve the issues mentioned above.

regard to 1. - this is not accurate.. The WhatsApp web indeed loads and sync old messages, but it takes some time when you first sign in. you can see in the last 2 images I've added to the issue. It's in Hebrew but the last one popped up after I clicked on a message which says "Older messages are being synchronized. Click here to view progress", and when I click on this notification, the last image popped up and show the progress of the syncing old messages...

anyway, tried the "fetchMessages" with big number as you said, and as i said earlier -> it give me different amount of messages each time since the syncing of older message is in progress...

@pedroslopez can you please help here?

themazim commented 1 year ago

Interesting, for me there is no syncing. It plainly states "use your phone to see old messages". Is this a setting in whatsapp?

MatanMaimon commented 1 year ago

Interesting, for me there is no syncing. It plainly states "use your phone to see old messages". Is this a setting in whatsapp?

i believe you don't see it because all your messages already loaded in someway before.. maybe try to go to "web.whatsapp.com" in incognito mode and login maybe then you will notice that