Overview
Baileys uses an EventEmitter-based architecture for all WhatsApp interactions. Every action - from receiving messages to connection updates - is emitted as a typed event.All events are fully typed with TypeScript, providing excellent IntelliSense support in modern editors like VS Code.
Event Architecture
Baileys provides two ways to handle events:- Individual Listeners
- Batch Processing
Core Events
Connection Events
connection.update
Emitted when connection state changes (connecting, open, close).creds.update
Emitted when authentication credentials are updated. Critical for session persistence.Message Events
messages.upsert
Receive new messages or message history.Important: Always use a loop like
for (const message of messages) to handle all messages in the array. The event may contain multiple messages.Message Types
notify- New message received while onlineappend- Message added to the end of historyprepend- Message added to the beginning of history
Auto-Reply Example
messages.update
Emitted when message metadata changes (delivered, read, deleted, edited, poll votes).Decrypt Poll Votes
messages.delete
Emitted when messages are deleted.messages.reaction
Emitted when a message receives a reaction.message-receipt.update
Emitted when message read/delivery status changes.Chat Events
chats.upsert
New chats added.chats.update
Chat metadata updated (unread count, last message, etc.).chats.delete
Chats deleted.Contact Events
contacts.upsert
New contacts added or discovered.contacts.update
Contact information updated (profile picture, name, etc.).Group Events
groups.upsert
New groups discovered.groups.update
Group metadata changed (subject, description, settings).group-participants.update
Group participants added, removed, promoted, or demoted.Presence Events
presence.update
User’s online/offline/typing status changed.Call Events
call
Incoming or outgoing call events.History Events
messaging-history.set
Received chat history after initial connection.Complete Event Handler Example
From the official example.ts:Event Summary on First Connection
1
connection.update (connecting)
Socket begins establishing connection to WhatsApp servers.
2
connection.update (qr or pairing)
QR code or pairing code available for authentication.
3
connection.update (open)
Connection successfully opened.
4
messaging-history.set
Chat history, contacts, and messages are synced.
5
chats.upsert / contacts.upsert
Individual chats and contacts are upserted.
Best Practices
1
Use Batch Processing
Prefer
sock.ev.process() over individual sock.ev.on() calls for better performance.2
Always Save Credentials
Listen to
creds.update and save immediately to prevent authentication issues.3
Loop Through Arrays
Always use
for loops when handling arrays in events like messages.upsert.4
Check Message Content
Always verify message content exists before processing to avoid null/undefined errors.
5
Handle Reconnections
Implement proper reconnection logic in
connection.update that distinguishes between logout and network failures.All Available Events
Here’s a complete list of all events in BaileysEventMap:Next Steps
Session Management
Implement getMessage for retry handling
Sending Messages
Learn how to send messages
Socket Configuration
Configure getMessage and other options