[GH-ISSUE #26] Backup session to AWS or similar to prevent deletion #21

Open
opened 2026-02-27 10:25:25 +03:00 by kerem · 1 comment
Owner

Originally created by @matan-d on GitHub (Dec 9, 2023).
Original GitHub issue: https://github.com/mimamch/wa-multi-session/issues/26

Hey,

Great Library and works perfect!

One issue, after I rebuild my Nodejs server all of the sessions get deleted.
Is this something that I can prevent?

I was thinking maybe to backup and restore the sessions to AWS S3.
Even got the saving part working
(currently im saving the entire WASocket object as I couldnt understand which part of it actually holds the token itself)

async function saveWhatsappSessionToAWS(sessionId, session) {
  console.log(`saveWhatsappSessionToAWS sessionId`, sessionId);
  console.log(`saveWhatsappSessionToAWS session`, session);

  // Convert the JSON object to a string
  const wasocketString = JSON.stringify(session);
  console.log(`saveWhatsappSessionToAWS wasocketString`, wasocketString);

  return new Promise(async (resolve, reject) => {
    const awsParams = {
      Body: wasocketString,
      Bucket: 'test',
      Key: `${sessionId}_wa_session`, // Modify Key structure as needed
    };

    // Create an S3 client
    const client = new S3Client({ region: 'us-east-1' });

    try {
      // Upload the session token content to S3
      await client.send(new PutObjectCommand(awsParams));
      console.log(`Session token for ${sessionId} uploaded to AWS S3.`);
      resolve(true)
    } catch (err) {
      console.error('Error uploading session token to S3:', err);
      resolve(false)
    }
  });
}

the problem is to retrieve it back:

async function loadWhatsappSessionsFromAWS(sessionId) {
  console.log(`loadWhatsappSessionsFromAWS sessionId`, sessionId);

  return new Promise(async (resolve, reject) => {
    const awsParams = {
      Bucket: 'test',
      Key: `${sessionId}_wa_session`, // Modify Key structure as needed
    };
    // Create an S3 client
    const client = new S3Client({ region: 'us-east-1' });

    try {
      // Retrieve the session token from S3
      const response = await client.send(new GetObjectCommand(awsParams));

      if (response.Body) {
        const sessionString = response.Body.toString('utf-8');
        console.log(`sessionString for ${sessionId} retrieved from AWS S3:\n`, sessionString);

        if (typeof sessionString === 'string' && sessionString.trim() !== '') {
          const session = JSON.parse(sessionString);
          console.log(`session for ${sessionId} retrieved from AWS S3:\n`, session);

          const waConnection = new WAConnection();
          waConnection.loadAuthInfo(session);
          console.log(`waConnection for ${sessionId} retrieved from AWS S3:\n`, waConnection);

          resolve(waConnection)
        } else {
          resolve(false)
        }
      } else {
        console.log(`Session token for ${sessionId} not found in AWS S3.`);
        resolve(false)
      }
    } catch (err) {
      console.error('Error retrieving session token from S3:', err);
      resolve(false)
    }
  });
}

the restore results in an error: "SyntaxError: Unexpected token o in JSON at position 1" because the WASocket probably gets corrupted while serializing..

any suggestions?

Originally created by @matan-d on GitHub (Dec 9, 2023). Original GitHub issue: https://github.com/mimamch/wa-multi-session/issues/26 Hey, Great Library and works perfect! One issue, after I rebuild my Nodejs server all of the sessions get deleted. Is this something that I can prevent? I was thinking maybe to backup and restore the sessions to AWS S3. Even got the saving part working _(currently im saving the entire WASocket object as I couldnt understand which part of it actually holds the token itself)_ ``` async function saveWhatsappSessionToAWS(sessionId, session) { console.log(`saveWhatsappSessionToAWS sessionId`, sessionId); console.log(`saveWhatsappSessionToAWS session`, session); // Convert the JSON object to a string const wasocketString = JSON.stringify(session); console.log(`saveWhatsappSessionToAWS wasocketString`, wasocketString); return new Promise(async (resolve, reject) => { const awsParams = { Body: wasocketString, Bucket: 'test', Key: `${sessionId}_wa_session`, // Modify Key structure as needed }; // Create an S3 client const client = new S3Client({ region: 'us-east-1' }); try { // Upload the session token content to S3 await client.send(new PutObjectCommand(awsParams)); console.log(`Session token for ${sessionId} uploaded to AWS S3.`); resolve(true) } catch (err) { console.error('Error uploading session token to S3:', err); resolve(false) } }); } ``` the problem is to retrieve it back: ``` async function loadWhatsappSessionsFromAWS(sessionId) { console.log(`loadWhatsappSessionsFromAWS sessionId`, sessionId); return new Promise(async (resolve, reject) => { const awsParams = { Bucket: 'test', Key: `${sessionId}_wa_session`, // Modify Key structure as needed }; // Create an S3 client const client = new S3Client({ region: 'us-east-1' }); try { // Retrieve the session token from S3 const response = await client.send(new GetObjectCommand(awsParams)); if (response.Body) { const sessionString = response.Body.toString('utf-8'); console.log(`sessionString for ${sessionId} retrieved from AWS S3:\n`, sessionString); if (typeof sessionString === 'string' && sessionString.trim() !== '') { const session = JSON.parse(sessionString); console.log(`session for ${sessionId} retrieved from AWS S3:\n`, session); const waConnection = new WAConnection(); waConnection.loadAuthInfo(session); console.log(`waConnection for ${sessionId} retrieved from AWS S3:\n`, waConnection); resolve(waConnection) } else { resolve(false) } } else { console.log(`Session token for ${sessionId} not found in AWS S3.`); resolve(false) } } catch (err) { console.error('Error retrieving session token from S3:', err); resolve(false) } }); } ``` the restore results in an error: "SyntaxError: Unexpected token o in JSON at position 1" because the WASocket probably gets corrupted while serializing.. any suggestions?
Author
Owner

@gillangit commented on GitHub (Mar 23, 2024):

or can we save session on mysql or mongodb?

<!-- gh-comment-id:2016491658 --> @gillangit commented on GitHub (Mar 23, 2024): or can we save session on mysql or mongodb?
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/wa-multi-session-mimamch#21
No description provided.