Store & Sessions
The in-memory store is the most-missed feature of classic Baileys โ in AunuBaileys it is back, complete and typed.
makeInMemoryStore
import { makeInMemoryStore } from 'aunubaileys'
const store = makeInMemoryStore({}) // options: { logger, chatKey, labelAssociationKey }
store.bind(sock.ev) // the store now follows every socket event
Store contents: chats (KeyedDB), contacts, per-JID messages, groupMetadata, connection state, presences, labels, and labelAssociations.
Most-used methods
| Method | Purpose |
|---|---|
store.loadMessage(jid, id) | Fetch one stored message โ the key to retries & quoting |
store.loadMessages(jid, count, cursor) | Load a chat's message history |
store.mostRecentMessage(jid) | The latest message in a chat |
store.fetchGroupMetadata(jid, sock) | Group metadata (store cache first, socket fallback) |
store.fetchImageUrl(jid, sock) | Profile picture URL |
store.fetchMessageReceipts(key) | Read receipts for a message |
store.getChatLabels(chatId) / getMessageLabels(id) | Labels per chat / per message |
store.toJSON() / store.fromJSON(json) | Serialize the whole store |
Persisting the store to a file
import fs from 'node:fs'
// save periodically
setInterval(() => {
fs.writeFileSync('./store.json', JSON.stringify(store.toJSON()))
}, 60_000)
// restore at startup (if the file exists)
if (fs.existsSync('./store.json')) {
store.fromJSON(JSON.parse(fs.readFileSync('./store.json', 'utf-8')))
}
Wiring the store for message retries
const sock = makeWASocket({
auth: state,
getMessage: async (key) => {
const msg = await store.loadMessage(key.remoteJid, key.id)
return msg?.message || undefined
}
})
Without getMessage, WhatsApp cannot ask for a re-send of a message that failed to decrypt โ and recipients see "waiting for this message".
Sessions: useMultiFileAuthState
const { state, saveCreds } = await useMultiFileAuthState('auth')
sock.ev.on('creds.update', saveCreds)
Atomic writes (v1.0.2): session files are written atomically in AunuBaileys โ a process dying mid-write no longer leaves half-written credential files. The compatibility exports
initInMemoryKeyStore() and processedMessages are also available for older scripts.