Skip to content

Latest commit

 

History

History
300 lines (210 loc) · 5.01 KB

File metadata and controls

300 lines (210 loc) · 5.01 KB

📦 makeInMemoryStore V1.2

A fully featured in-memory state manager designed as a drop-in replacement for the deprecated makeInMemoryStore from the latest versions of Baileys (WhatsApp Web API library).

Built specifically for WhatsApp Multi-Device bots using the modern event-driven architecture.


🚀 Why This Exists

The original makeInMemoryStore was removed from newer versions of Baileys. Many bots still rely on it for:

  • Contact caching
  • Chat indexing
  • Message history tracking
  • Presence updates
  • Group metadata storage
  • Call state tracking
  • Auth state syncing

This module restores that functionality — with improvements and modern safeguards.


✨ Features

📇 Contacts

  • setContacts()
  • upsertContact()
  • updateContact()
  • deleteContact()

💬 Chats

  • setChats()
  • upsertChat()
  • updateChat()
  • deleteChat()

📨 Messages

  • setMessages()
  • upsertMessage()
  • updateMessage()
  • deleteMessage()
  • loadMessage()

Supports:

  • Poll updates
  • Message merges (preserves old data)
  • Optional per-chat message limit (memory safeguard)
  • Event emission compatibility

👀 Presence Tracking

  • setPresence()
  • updatePresence()

Tracks per-chat and per-participant presence state.

👥 Group Metadata

  • setGroupMetadata()
  • updateGroupMetadata()

📞 Call Offers

  • setCallOffer()
  • clearCallOffer()

🎨 Sticker Packs

  • setStickerPacks()
  • upsertStickerPack()

🔐 Authentication State

  • setAuthState()
  • getAuthState()

🔄 History Sync Tracking

  • markHistorySynced()
  • isHistorySynced()

💾 Persistence Helpers

  • load(state)
  • save()
  • clear()

🔗 Event Binding

  • bind(ev)
  • unbind(ev) (optional safety cleanup)

Fully compatible with Baileys socket event emitter.


🧠 Architecture Overview

This store:

  • Extends EventEmitter
  • Maintains internal indexed objects for fast lookup
  • Emits events similar to Baileys
  • Supports manual persistence
  • Includes safe guards & validation
  • Optionally limits message cache per chat
  • Works with modern Baileys socket events

⚠️ IMPORTANT:
This is an in-memory store.
All data is lost when the process restarts unless you manually persist it.


📥 Installation

Install recommended logger:

npm install pino

Then copy store.js into your project.


⚙️ Basic Usage

// Require file
const makeInMemoryStore = require('./store')
const pino = require('pino')

// Create store
const store = makeInMemoryStore({
    logger: pino({ level: 'silent' }),
    maxMessagesPerChat: 500 // optional
})

🔌 Bind to Baileys Socket

const sock = makeWASocket({ ... })

store.bind(sock.ev)

Once bound, the store will automatically update itself when:

  • Contacts change
  • Messages arrive
  • Groups update
  • Presence updates
  • Call events fire
  • History sync completes

No manual syncing required.

If your socket reconnects or reloads, you may safely call:

store.unbind(sock.ev)

before re-binding to prevent duplicate listeners.


💾 Optional: Manual Persistence

Since this is memory-only, you can persist it manually.

Save to File

const fs = require('fs')

setInterval(() => {
    const state = store.save()
    fs.writeFileSync('./store.json', JSON.stringify(state))
}, 10000)

Load From File

if (fs.existsSync('./store.json')) {
    const data = JSON.parse(fs.readFileSync('./store.json'))
    store.load(data)
}

📚 API Reference

Contacts

store.contacts
store.upsertContact(contact)
store.deleteContact([id])

Chats

store.chats
store.upsertChat(chat)
store.deleteChat([id])

Messages

store.messages
store.loadMessage(jid, messageId)
store.upsertMessage(message)

Presence

store.presences
store.setPresence(chatId, presence)

Groups

store.groupMetadata

Auth

store.getAuthState()

🛠 Production Recommendations

If you are running a large-scale bot:

  • Persist store regularly
  • Consider limiting stored messages per chat
  • Monitor memory usage
  • Use proper logging levels (pino supported)

For heavy production bots, you may consider migrating to:

  • Redis
  • MongoDB
  • PostgreSQL

For small–medium bots, this store is extremely fast and efficient.


🧪 Recommended Use Cases

✅ Development
✅ Testing
✅ Lightweight bots
✅ Temporary session bots
⚠️ Not ideal for multi-instance clustering without external sync


📊 Performance Characteristics

  • O(1) object access
  • Fast message lookup
  • No disk I/O overhead
  • Fully event-driven
  • Minimal dependencies
  • Optional in-memory safeguards for long-running processes

🧩 Compatibility

Designed for:

  • Latest Multi-Device Baileys versions
  • Node.js 16+

👨‍💻 Developer

Maxz
https://linktr.ee/dcodemaxz


📜 License

Custom implementation inspired by deprecated Baileys store system.
Free for personal and commercial use.