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

Ready event is broken #3181

Closed
1 task done
irissonnlima opened this issue Jul 3, 2024 · 18 comments
Closed
1 task done

Ready event is broken #3181

irissonnlima opened this issue Jul 3, 2024 · 18 comments
Labels
bug Something isn't working

Comments

@irissonnlima
Copy link

irissonnlima commented Jul 3, 2024

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

const { MongoStore } = require("wwebjs-mongo");
const mongoose = require("mongoose");
const { RemoteAuth, Client } = require("whatsapp-web.js");

class WhatsappClient {
  constructor(containerId, userId) {
    this.mongoURI = "mongodb://mongo-uri/";
    this.containerId = containerId;
    this.userId = userId;
    this.client = null;
    this.clientReady = false;
  }

  getContainerId() {
    return this.containerId;
  }

  getUserId() {
    return this.userId;
  }

  setClient(client) {
    if (client) {
      this.client = client;
    } else {
      console.log("setClient: Client inexistente!");
    }
  }

  setReadyClient(state) {
    this.clientReady = state;
  }

  createClientConnection() {
    return new Promise(async (resolve, reject) => {
      const clientId = this.containerId;

      console.log("DADOS OBTIDOS DA CHAMADA API CLIENT:", clientId);

      const mongoURI =
        process.env.MONGO_URI ??
        "mongodb://mongo-uri/";
      await mongoose.connect(mongoURI);
      const store = new MongoStore({ mongoose: mongoose });

      const client = new Client({
        puppeteer: {
          args: ["--no-sandbox"],
        },
        authStrategy: new RemoteAuth({
          clientId: `${clientId}`,
          store: store,
          backupSyncIntervalMs: 300000,
        }),
      });

      client.on("qr", (qr) => {
        console.log("Qrcode gerado!");
        qrcode.generate(qr, { small: true });
      });

      client.on("authenticated", async () => {
        console.log("Authenticated");
      });

      client.once("ready", () => {
        console.log("Client is ready!");
        console.log(client.info);
      });

      client.initialize();
    });
  }
}

module.exports = {
  WhatsappClient,
};

After the authenticated event is successfully triggered, the code encounters an error and does not reach the ready event. The reported error is:

Error: Execution context was destroyed, most likely because of a navigation.
    at rewriteError (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:284:15)
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async ExecutionContext._ExecutionContext_evaluate (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:227:56)
    at async ExecutionContext.evaluate (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:107:16)
    at async Client.inject (/src/node_modules/whatsapp-web.js/src/Client.js:101:13)
    at async /src/node_modules/whatsapp-web.js/src/Client.js:350:13

I'm using the version "whatsapp-web.js": "github:pedroslopez/whatsapp-web.js#webpack-exodus".

When the authenticated event happens on my phone, it appears active, but the ready event never occurs.

Expected behavior

I expected the 'ready' event to be triggered, but this did not happen.

Steps to Reproduce the Bug or Issue

Try to instantiate the class and run createClientConnection

Relevant Code

No response

Browser Type

Chromium

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

FROM node:18.13.0

WORKDIR /src

# Instale as dependências adicionais necessárias para executar o Chrome no webjs
RUN apt-get update -y \
    && apt-get install -y \
    gconf-service \
    libgbm-dev \
    libasound2 \
    libatk1.0-0 \
    libc6 \
    libcairo2 \
    libcups2 \
    libdbus-1-3 \
    libexpat1 \
    libfontconfig1 \
    libgcc1 \
    libgconf-2-4 \
    libgdk-pixbuf2.0-0 \
    libglib2.0-0 \
    libgtk-3-0 \
    libnspr4 \
    libpango-1.0-0 \
    libpangocairo-1.0-0 \
    libstdc++6 \
    libx11-6 \
    libx11-xcb1 \
    libxcb1 \
    libxcomposite1 \
    libxcursor1 \
    libxdamage1 \
    libxext6 \
    libxfixes3 \
    libxi6 \
    libxrandr2 \
    libxrender1 \
    libxss1 \
    libxtst6 \
    ca-certificates \
    fonts-liberation \
    libappindicator1 \
    libnss3 \
    lsb-release \
    xdg-utils \
    wget

# Copiando arquivos
COPY ./package.json ./package-lock.json* ./

# Instalando dependências
RUN npm init -y && \
    npm install github:pedroslopez/whatsapp-web.js#webpack-exodus qrcode-terminal mongoose wwebjs-mongo mysql2 amqplib

COPY ./src .

EXPOSE 7001

CMD ["node", "app.js"]

Additional context

No response

@irissonnlima irissonnlima added the bug Something isn't working label Jul 3, 2024
@irissonnlima
Copy link
Author

I tried again and received:

Criando conexão WhatsApp...
DADOS OBTIDOS DA CHAMADA API CLIENT: c1c58233-2e04-49b8-805d-8dc02e5b9190
Authenticated
Client is ready!
ClientInfo {
my-client-info
}
/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:284
throw new Error('Execution context was destroyed, most likely because of a navigation.');
^

Error: Execution context was destroyed, most likely because of a navigation.
at rewriteError (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:284:15)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async ExecutionContext._ExecutionContext_evaluate (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:227:56)
at async ExecutionContext.evaluate (/src/node_modules/puppeteer-core/lib/cjs/puppeteer/common/ExecutionContext.js:107:16)
at async Client.inject (/src/node_modules/whatsapp-web.js/src/Client.js:251:37)
at async /src/node_modules/whatsapp-web.js/src/Client.js:350:13

Node.js v18.13.0

@ntaraujo
Copy link

ntaraujo commented Jul 3, 2024

I'm having the same issue

@PeccinCodes

This comment was marked as duplicate.

@victoromarques

This comment was marked as duplicate.

1 similar comment
@alexfarfam

This comment was marked as duplicate.

@victoromarques

This comment was marked as off-topic.

@JuniorCruzEG
Copy link

Has anyone managed to solve the ready event?

@ntaraujo
Copy link

ntaraujo commented Jul 4, 2024

solved with this

#2816 (comment)

@victoromarques
Copy link

resolvido com isso

#2816 (comentário)

this worked for me, thank you very much!

@irissonnlima
Copy link
Author

Not working yet

@gastonrobledo
Copy link

is not working, it works if I do the authentication intially but if I want to restore a session (remote) ready event is not triggered and 'authentication' event is being triggered twice, I was using github:pedroslopez/whatsapp-web.js#webpack-exodus

@irissonnlima
Copy link
Author

To assist the community and library developers, I managed to get it running.

I tried to capture the error but was unsuccessful. I am running in a Kubernetes environment, and when attempting to run the bot with 1vCPU and 2048MB of RAM, the bot encountered the reported error. However, when limiting the resources to 0.5vCPU and 1024MB of RAM, the bot no longer encountered errors during startup and ran smoothly. I was able to replicate the error using LocalAuth instead of RemoteAuth.

Another important point I noticed was that if the process doesn't use cache, the error does not occur. In other words, if I start a new session, the entire process runs naturally, but this is inconvenient as it's susceptible to restarts of my machine.

@max-programming
Copy link

For me, whenever I log out from the phone (or remove device - same thing), it fires the ready event for some reason

@srkinha

This comment was marked as off-topic.

@dv-sys

This comment was marked as off-topic.

@victoromarques

This comment was marked as duplicate.

@kkheir

This comment was marked as outdated.

@bymarcelo

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests