Whatsapp Webjs Wont Reconnect After Macbook Sleep

You’re facing an issue with the whatsapp-web.js library when trying to reconnect after your MacBook goes to sleep. The solution you’ve provided involves using a remote browser via Docker to run Chrome and then connecting whatsapp-web.js to it using the browserWSEndpoint option. This is a workaround to ensure that the connection is maintained even after your MacBook wakes up from sleep mode.

Here’s a breakdown of the steps and code you’ve provided:

Issue Description

You mentioned that when you run whatsapp-web.js with Chromium and then put your MacBook to sleep, the application is unable to reconnect to Chromium when your MacBook wakes up. This results in the application staying idle even when new messages arrive.

Proposed Solution

To solve this issue, you suggested using a remote browser by running Chrome in Docker and opening a port for Puppeteer to connect to. Here’s how you can set it up:

Docker Compose Configuration (docker-compose.yml)

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
version: "3"
services:
  chrome:
    restart: always
    image: browserless/chrome:latest
    environment:
      - MAX_CONCURRENT_SESSIONS=1

  app:
    restart: always
    build: .
    depends_on:
      - "chrome"

In this configuration:

  • You define two services, chrome and app.
  • The chrome service uses the browserless/chrome:latest Docker image, ensuring that Chrome is available for your application to connect to.
  • You set the environment variable MAX_CONCURRENT_SESSIONS to 1 to limit the number of concurrent browser sessions to one.

app.js Code

In your app.js file, you configure the whatsapp-web.js client to use the remote browser opened by Docker:

1
2
3
4
5
6
const client = new Client({
  puppeteer: {
    browserWSEndpoint: 'ws://chrome:3000', // Connect to the remote browser
  },
  session: sessionCfg, // Your session configuration
});

Here, you set the browserWSEndpoint option to the WebSocket address of the remote Chrome instance running in Docker, which allows whatsapp-web.js to connect to it.

Summary

By using this Docker-based approach, you can ensure that whatsapp-web.js maintains a stable connection to Chromium even after your MacBook wakes up from sleep mode. This should prevent the issue of the application becoming idle when new messages arrive.

0%