Saving multi session baileys into an external database to save resources.
- PostgreSQL
- SQL Lite (Recommended)
- MySQL
- MongoDB
- Firebase (Firestore) (Recommended)
The following is the installation for each database.
Add this to dependencies package.json
:
"session": "github:neoxr/session#postgresql"
Then call the default function in your bot connection file
const { usePostgresAuthState } = require('session')
async function start() {
const { state, saveCreds, deleteCreds, autoDeleteOldData } = await usePostgresAuthState('postgres://xxxxx', 'session', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
const client = makeWASocket({
// your configuration
})
client.ev.on('connection.update', async (session) => {
if (session.reason === 401) {
await deleteCreds()
throw new Error('Device Logout')
}
})
client.ev.on('creds.update', saveCreds)
setInterval(async () => {
await autoDeleteOldData()
}, 1 * 60 * 60 * 1000) // checking every 1 hour
// your code ...
}
Add this to dependencies package.json
:
"session": "github:neoxr/session#sqlite"
Then call the default function in your bot connection file
const { useSQLiteAuthState } = require('session')
async function start() {
const { state, saveCreds, deleteCreds, autoDeleteOldData } = await useSQLiteAuthState('session.db', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
const client = makeWASocket({
// your configuration
})
client.ev.on('connection.update', async (session) => {
if (session.reason === 401) {
await deleteCreds()
throw new Error('Device Logout')
}
})
client.ev.on('creds.update', saveCreds)
setInterval(async () => {
await autoDeleteOldData()
}, 1 * 60 * 60 * 1000) // checking every 1 hour
// your code ...
}
Add this to dependencies package.json
:
"session": "github:neoxr/session#mysql"
Then call the default function in your bot connection file
const { useMySQLAuthState } = require('session')
async function start() {
const { state, saveCreds, deleteCreds, autoDeleteOldData } = await useMySQLAuthState('mysql://xxxxx', 'session', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
const client = makeWASocket({
// your configuration
})
client.ev.on('connection.update', async (session) => {
if (session.reason === 401) {
await deleteCreds()
throw new Error('Device Logout')
}
})
client.ev.on('creds.update', saveCreds)
setInterval(async () => {
await autoDeleteOldData()
}, 1 * 60 * 60 * 1000) // checking every 1 hour
// your code ...
}
Add this to dependencies package.json
:
"session": "github:neoxr/session#mongo"
Then call the default function in your bot connection file
const { useMongoAuthState } = require('session')
async function start() {
const { state, saveCreds, deleteCreds, autoDeleteOldData } = await useMongoAuthState('mongodb://xxxxx', 'session', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
const client = makeWASocket({
// your configuration
})
client.ev.on('connection.update', async (session) => {
if (session.reason === 401) {
await deleteCreds()
throw new Error('Device Logout')
}
})
client.ev.on('creds.update', saveCreds)
setInterval(async () => {
await autoDeleteOldData()
}, 1 * 60 * 60 * 1000) // checking every 1 hour
// your code ...
}
Add this to dependencies package.json
:
"session": "github:neoxr/session#firebase"
Then call the default function in your bot connection file
const { useFirebaseAuthState } = require('session')
const fs = require('fs')
const firebaseConfig = JSON.parse(fs.readFileSync('./firebase.json', 'utf-8))
async function start() {
const { state, saveCreds, deleteCreds, autoDeleteOldData } = await useFirebaseAuthState(firebaseConfig, 'session', 5 * 60 * 60 * 1000) // set maxAge default 24 hours, but this example is 5 hours
const client = makeWASocket({
// your configuration
})
client.ev.on('connection.update', async (session) => {
if (session.reason === 401) {
await deleteCreds()
throw new Error('Device Logout')
}
})
client.ev.on('creds.update', saveCreds)
setInterval(async () => {
await autoDeleteOldData()
}, 1 * 60 * 60 * 1000) // checking every 1 hour
// your code ...
}
Notes :
-
deleteCreds
: used when the device logout or certain conditions require re-pairing -
autoDeleteOldData
: used to delete sessions by excluding creds and app-state-sync
If there is an error make an issue, this script was made by neoxr and I hope this script is useful for everyone.