Whatsapp-web.js: [question] - How to avoid duplicated session

Created on 28 Aug 2020  路  2Comments  路  Source: pedroslopez/whatsapp-web.js

I have this code for multiple sessions:

const client = {
  /* init app instance */
  init: async (id, instanceConfig, headless = false) => {

    /* Recover session file if user already has logged */
    var filePath = [clientConfig.WHATSAPP.SESSION_FILE_PATH, id, '/session_', id, 'json']
    if (fs.existsSync(filePath.join(""))) {
      session_file = require(filePath.join(""));
    }

    /* Client options */
    const clientOptions = {
      puppeteer: {
        headless: headless,
        userDataDir: filePath.slice(0, 2).join(""),
        args: clientConfig.WHATSAPP.BROWSER_ARGS,
      },
      takeoverOnConflict: true,
      takeoverTimeoutMs: 0,
      session: session_file
    };

    sessions[id] = new Client(clientOptions)
    sessions[id].initialize();

    return sessions[id]
  }
}

The code above, allows me to init multiple sessions with the following function:

client.init('123456') //this is the session id

This works fine but if i call another client.init('123456') - duplicated id - then i have this error and the browser open a blank tab

(node:12704) UnhandledPromiseRejectionWarning: Error: Failed to launch the browser process!
[6044:7980:0828/004251.563:ERROR:cache_util_win.cc(21)] Unable to move the cache: Acesso negado. (0x5)
[6044:7980:0828/004251.563:ERROR:cache_util.cc(139)] Unable to move cache folder C:\wamp64\www\whatsapp\se
ssions\11969157483\ShaderCache\GPUCache to C:\wamp64\www\whatsapp\sessions\11969157483\ShaderCache\old_GPU
Cache_000
[6044:7980:0828/004251.563:ERROR:disk_cache.cc(184)] Unable to create cache
[6044:7980:0828/004251.563:ERROR:shader_disk_cache.cc(606)] Shader Cache Creation failed: -2

TROUBLESHOOTING: https://github.com/puppeteer/puppeteer/blob/master/docs/troubleshooting.md

    at onClose (C:\wamp64\www\whatsapp\node_modules\puppeteer\lib\launcher\BrowserRunner.js:159:20)
    at ChildProcess.<anonymous> (C:\wamp64\www\whatsapp\node_modules\puppeteer\lib\launcher\BrowserRunner.
js:150:76)
    at ChildProcess.emit (events.js:327:22)
: 1)
(node:12704) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, pro
mise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
Deseja finalizar o arquivo em lotes (S/N)? ^C
PS C:\wamp64\www\whatsapp> ^C
PS C:\wamp64\www\whatsapp> ^C
PS C:\wamp64\www\whatsapp> ^C
    at ChildProcess.emit (events.js:327:22)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)
(node:13592) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either b
y throwing inside of an async function without a catch block, or by rejecting a promise which was not hand
led with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhan
dled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id
: 1)
(node:13592) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, pro
mise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

There is a way to avoid this?
There is a way to check if specific session have a puppeteer session active/open?

bug

Most helpful comment

I've found a "solution".

On my Client object i created a function called pingSession() that check if pupBrowser is connected.

const client = {

    pingSession: async (id) => {

        if(sessions[id] === undefined) return false

            return await sessions[id].pupBrowser.isConnected()

    },
}

This function will return true if already exist a browser opened on given session id or false if not.
This function i use before of my function client.init() that is used to initiate the Client like this:

  var ping = await client.pingSession(id)
  if (ping) {

    console.log('InstanceId: %s already open', id)
    return false

  } else {

    var init = await client.init(id, configs)

}

If someone else have a different approach, i'll be glad to see 馃槃

All 2 comments

I've found a "solution".

On my Client object i created a function called pingSession() that check if pupBrowser is connected.

const client = {

    pingSession: async (id) => {

        if(sessions[id] === undefined) return false

            return await sessions[id].pupBrowser.isConnected()

    },
}

This function will return true if already exist a browser opened on given session id or false if not.
This function i use before of my function client.init() that is used to initiate the Client like this:

  var ping = await client.pingSession(id)
  if (ping) {

    console.log('InstanceId: %s already open', id)
    return false

  } else {

    var init = await client.init(id, configs)

}

If someone else have a different approach, i'll be glad to see 馃槃

I believe your specific issue is due to using the same userDataDir for two sessions, which could be addressed a couple of different ways in the implementation.

Your solution is probably fine, but it would be good to have a method in the library that checks this for you. This was already suggested in #268, so I'll close this as a duplicated issue.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

waltergammarota picture waltergammarota  路  8Comments

felipemm picture felipemm  路  5Comments

franciscol99 picture franciscol99  路  3Comments

dedenhendrap picture dedenhendrap  路  8Comments

centralderastreamentogps picture centralderastreamentogps  路  7Comments