Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How about creating an onReady event to get better how we fetch messages? #2419

Open
huogerac opened this issue Dec 12, 2024 · 3 comments
Open
Labels
enhancement New feature or request needs triage

Comments

@huogerac
Copy link

huogerac commented Dec 12, 2024

problem?
When we start the client, in the first time I'd like to fetch all chats and messages, so when I call the client.getAllChats() it was returning 0 chats: [], after playing a little bit and adding a TimeOut it worked:

import { create } from '@wppconnect-team/wppconnect';
const options {...}
create(options).then((client) => {
...

  setTimeout( () => {
    try {
      client.getAllChats().then((chats) => {
        console.log(chats)
      })
    } catch (error) {
      console.error('Error:', error);
    }
  }, 6000)

One idea of solution
In the same way there are some events in the ListenerLayer such as onMessage and other, perhaps
it could be added something like onReady which coulbe be sent when the chat that is ready to be be called.
In this way, I could call the client.getAllChats() inside this event instead of the timeout of 6 seconds in the initialize step.

Alternatives
Well, if I am doing something wrong to get the chats when I start the app (after I the phone connected using the qr code), please let me know, or if is there any documentation or any advanced examples I will appreciate...

Context
I can receive new messages and updates, however, I'd like to map all existing chats and messages when the application runs for the first time, after that, I won't need to call the allChats function any more.

Thanks and I will appreciate any other idea or further documentation and or examples as other way to avoid that 6 seconds.

p.s.
I was digging the documentation and the existing listeners (ListenerLayer from the listener.layer.d.ts file) before creating this Issue.

@huogerac huogerac added enhancement New feature or request needs triage labels Dec 12, 2024
@huogerac huogerac changed the title How about creating a onReady event get better how we fetch messages? How about creating an onReady event get better how we fetch messages? Dec 12, 2024
@huogerac huogerac changed the title How about creating an onReady event get better how we fetch messages? How about creating an onReady event to get better how we fetch messages? Dec 12, 2024
@huogerac
Copy link
Author

Perhaps another way it would be wait the CONNECTED status, but it seems not all the time we get this status change...

  client.onStateChange((status) => {
    if (status == 'CONNECTED') {
      try {
        client.getAllChats().then((chats) => {
          console.log(chats)
        })
      } catch (error) {
        console.error('Error:', error);
      }
    }
  });

@Saifallak
Copy link

Perhaps another way it would be wait the CONNECTED status, but it seems not all the time we get this status change...

  client.onStateChange((status) => {
    if (status == 'CONNECTED') {
      try {
        client.getAllChats().then((chats) => {
          console.log(chats)
        })
      } catch (error) {
        console.error('Error:', error);
      }
    }
  });

Connected or inChat

@huogerac
Copy link
Author

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request needs triage
Projects
None yet
Development

No branches or pull requests

2 participants