WhiskeySockets / Baileys

Lightweight full-featured typescript/javascript WhatsApp Web API

Home Page:https://baileys.whiskeysockets.io/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[BUG] Frequent web socket closed

oscarguindzberg opened this issue · comments

Describe the bug
I have a server with 15 connected whatsapp users.
WebSockets are closed from time to time.
This callback is executed:

ws.on('close', () => end(new Boom('Connection Terminated', { statusCode: DisconnectReason.connectionClosed })))

Then baileys generates a 'connection.update' event where connection === 'close'

lastDisconnect: {
  error: Error: Connection Terminated
      at WebSocketClient.<anonymous> (/node_modules/myVersionOfBaileys/baileys/lib/Socket/socket.js:459:30)
      at WebSocketClient.emit (node:events:518:28)
      at WebSocket.<anonymous> (/node_modules/myVersionOfBaileys/baileys/lib/Socket/Client/web-socket-client.js:46:100)
      at WebSocket.emit (node:events:518:28)
      at WebSocket.emitClose (/node_modules/ws/lib/websocket.js:265:10)
      at TLSSocket.socketOnClose (/node_modules/ws/lib/websocket.js:1289:15)
      at TLSSocket.emit (node:events:530:35)
      at node:net:337:12
      at TCP.done (node:_tls_wrap:657:7) {
    data: null,
    isBoom: true,
    isServer: false,
    output: [Object]
  },
  date: 2024-06-14T05:04:58.648Z
}

After the websocket is closed, I do an automatic reconnection and it works fine.

To Reproduce
Steps to reproduce the behavior:
Don't know how to reproduce, it happens from time to time on a server with 15 connected whatsapp users.

Expected behavior
No errors on the log

Environment (please complete the following information):

  • Is this on a server? yes
  • What do your connectOptions look like?
         const { version, isLatest } = await fetchLatestBaileysVersion()
        const { state, saveCreds } = await useMultiFileAuthState('myPath')
    const msgRetryCounterCache = new NodeCache()

        async function getMessage(key)  {
            if(store) {
                const msg = await store.loadMessage(key.remoteJid, key.id)
                return msg?.message || undefined
            }
            // only if store is present
            return proto.Message.fromObject({})
        }

 makeWASocket({
            version,
            logger,
            printQRInTerminal: false,
            mobile: false,
            auth: {
                creds: state.creds,
                keys: makeCacheableSignalKeyStore(state.keys, logger),
            },
            msgRetryCounterCache,
            generateHighQualityLinkPreview: true,
            // ignore broadcast and newsletter messages
            shouldIgnoreJid: jid => !jid || isJidBroadcast(jid) || isJidNewsletter(jid),
            // implement to handle retries & poll updates
            getMessage,
            shouldSyncHistoryMessage: () => true,
            markOnlineOnConnect: false
        })
  • Do you have multiple clients on the same IP? yes, 15 clients
  • Are you using a proxy? no

Additional context
Using baileys v6.7.5

Maybe related #884

I don't understand why was the issue closed.

You spammed 7 issues with basically same information. Also this is normal behaviour. You can observe whatsapp web and it will reconnect every now and then also.

I created 6 issues, some look similar to others, but each one is different. Error msg is different or a different exception is thrown, etc.
On each issue I included links to other issues that may be related.

About this issue in particular:
Does whatsapp web reconnect every now and then because it decides to do so?
In this particular case, it seems the server is closing the connection.