Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

191
Views
How to send qr code string response in whatsapp-web.js?

I'm trying to send the qr code of whatsapp-web.js to my frontend with the code bellow

const router = require("express").Router();

router.get("/", async (req: any, res: any) => {
  const qrcode = require("qrcode-terminal");

  const { Client, LocalAuth } = require("whatsapp-web.js");

  const client = new Client({
    authStrategy: new LocalAuth(),
  });
  client.on("qr", (qr: any) => {
    // qrcode.generate(qr, { small: true });

    res.send(qr)
  });

  client.on("ready", (qr:any) => {
    console.log("Client is ready!");

    const number = [
      { phone: "number1", name: "name1" },
      { phone: "number2", name: "name2" },
    ];

    number.forEach((el) => {

      // Your message.
      const text = `teste ${el.name}`;

      // Getting chatId from the number.
      // we have to delete "+" from the beginning and add "@c.us" at the end of the number.
      const chatId = el.phone.substring(1) + "@c.us";

      // Sending message.
      client.sendMessage(chatId, text);
    });
  });

  client.initialize();

  res.status(200).json("ok");
});

module.exports = router;

but it's not sending. Maybe because there's a delay to generate this code. Can you guys help me

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You're registering a callback that sends the qr when the "qr" event is emitted. The res.status(200) is executed before the event is emitted (Even if it is emitted immediately the callback will be executed after the rest of the function since Node is single threaded).

I'm not familiar with the whatsapp package but if you know the qr event will only be emitted once or you only need the first emission you can wrap it in a promise.

Try

router.get('/', async (req, res) => {
    const client = new Client(...)
    let qr = await new Promise((resolve, reject) => {
        client.once('qr', (qr) => resolve(qr))
    })
    res.send(qr)
}

Notice, I used client.once so the callback is unregistered from the client's event after one event is emitted to prevent memory leaks.

It is a good practice to add a reject scenario to prevent cases the promise doesn't resolve

router.get('/', async (req, res) => {
    try {
        const client = new Client(...)
        let qr = await new Promise((resolve, reject) => {
            client.once('qr', (qr) => resolve(qr))
            setTimeout(() => {
                reject(new Error("QR event wasn't emitted in 15 seconds."))
            }, 15000)
        })
        res.send(qr)
    } catch (err) {
        res.send(err.message)
    }
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!