whatsapp-web.js: QR code genration taking much time and other event listener not working

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

I’m genrating request to make qr code to scann it , but its taking much longer time

here is my full code please check once :

const express = require('express');
const { Client } = require('whatsapp-web.js');
const qrcode = require('qrcode');
const fs = require('fs');

const app = express();
const port = 3000;
const SESSION_FILE_PATH = 'YourSessionName.json';

let client;

// Initialize WhatsApp client
if (fs.existsSync(SESSION_FILE_PATH)) {
  // If a session file exists, load the session
  const sessionData = require(SESSION_FILE_PATH);
  client = new Client({ session: sessionData });
} else {
  client = new Client();
}

// QR Code generation and display
app.get('/', (req, res) => {
  if (!client.session) {
    client.on('qr', (qrCode) => {
      qrcode.toFileStream(res, qrCode);
    });
    client.initialize();
  } else {
    res.send('WhatsApp Web is already connected.');
  }
});

// Event listener for incoming messages
client.on('message', (message) => {
  // Handle incoming message
  console.log(`Received message from ${message.from}: ${message.body}`);
  
  // Send a reply
  client.sendMessage(message.from, 'Your reply goes here');
});

// Event listener for authentication
client.on('authenticated', (session) => {
  console.log('Authenticated!');
  fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session));
});

// Start the Express server
app.listen(port, () => {
  console.log(`Server is running on port ${port}`);
});

// Start the WhatsApp client
client.initialize();

When we scanned than this code not showing console:

client.on('authenticated', (session) => {
  console.log('Authenticated!');
  fs.writeFileSync(SESSION_FILE_PATH, JSON.stringify(session));
});

Expected behavior

Qr code should be genrated fast

Steps to Reproduce the Bug or Issue

i have provide above full code , use that and run here is my package.json

{
  "name": "whatsappweb",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "axios": "^1.5.1",
    "ejs": "^3.1.9",
    "express": "^4.18.2",
    "qrcode": "^1.5.3",
    "qrcode-terminal": "^0.12.0",
    "whatsapp-web.js": "^1.22.1"
  }
}

Relevant Code

No response

Browser Type

Google Chrome

WhatsApp Account Type

WhatsApp Business

Does your WhatsApp account have multidevice enabled?

No, I am not using Multi Device

Environment

OS: windows Phone OS: Android whatsapp-web.js version : ^1.22.1 Nodejs: ^18

Additional context

in my laptop have 4gb RAM and 256 GB SSD (Compac book)

About this issue

  • Original URL
  • State: closed
  • Created 9 months ago
  • Comments: 31

Most upvoted comments

try this instead :

npm i https://github.com/pedroslopez/whatsapp-web.js#main

Have you tried using the last main branch version which is currenltly in alpha instead?

There a re quite a lot of fixes implemented and it works on my end.

npm i whatsapp-web.js#main

Same Issue I am facing. How much time it will take to fix it

I think this repo is deprecated

On Mon, 16 Oct 2023, 6:10 pm jgHax, @.***> wrote:

Same issue here 😦

— Reply to this email directly, view it on GitHub https://github.com/pedroslopez/whatsapp-web.js/issues/2586#issuecomment-1764387515, or unsubscribe https://github.com/notifications/unsubscribe-auth/ALY3KNABKR44JCAZZ27CY33X7UTKVAVCNFSM6AAAAAA576UCFWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTONRUGM4DONJRGU . You are receiving this because you authored the thread.Message ID: @.***>

Same issue here 😦

Please check your npm version and update. Should work fine:

> npm i https://github.com/pedroslopez/whatsapp-web.js#main

up to date, audited 486 packages in 3s

29 packages are looking for funding
  run `npm fund` for details

found 0 vulnerabilities