# this-xys/baileys architecture

> Architecture diagram of the GitHub repository this-xys/baileys, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-04-16). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/this-xys/baileys
- Repository: https://github.com/this-xys/baileys

## Overview

1. **What this repo is**
   - This is a **Node.js library for talking to WhatsApp Web**, not an app server or UI.
   - The main public surface is in `lib/index.js`, which re-exports socket/client APIs, stores, auth-state helpers, protocol types, and utilities.

2. **Protocol/schema layer**
   - `WAProto/` contains the **generated protobuf schema** and related generation scripts.
   - This is the schema boundary for WhatsApp payloads: higher layers build/consume strongly typed message structures from here.
   - `lib/WABinary/` handles **wire-format encoding/decoding** for WhatsApp’s binary protocol, translating between raw socket frames and structured objects.

3. **Connection and transport**
   - `lib/Socket/Client/websocket.js` is the transport adapter over WebSocket.
   - `lib/Socket/socket.js` and `lib/Socket/Client/index.js` orchestrate the **session lifecycle**: connect, reconnect, authenticate, and route events.
   - `engine-requirements.js` suggests runtime compatibility checks are part of startup validation.

4. **Authentication and secure session setup**
   - `lib/Utils/noise-handler.js`, `crypto.js`, `auth-utils.js`, and `validate-connection.js` implement the **Noise-based handshake and connection validation** used by WhatsApp Web.
   - `lib/Signal/` contains the **Signal protocol implementation** for key exchange and message encryption, including group sender keys and pre-key handling.
   - This is the security boundary between the library and WhatsApp’s servers.

5. **Message send/receive pipeline**
   - `lib/Socket/messages-send.js` and `lib/Socket/messages-recv.js` are the core message ingress/egress paths.
   - `lib/Utils/process-message.js`, `decode-wa-message.js`, `message-retry-manager.js`, and `pre-key-manager.js` handle normalization, retries, decryption-related flows, and message interpretation.
   - Media and link-preview helpers live in `lib/Utils/messages-media.js`, `audioToBuffer.js`, and `link-preview.js`.

6. **Feature-specific domain facades**
   - `lib/Socket/chats.js`, `groups.js`, `communities.js`, `newsletter.js`, `business.js`, `mex.js`, and `usync.js` expose **domain APIs** on top of the socket.
   - These modules map WhatsApp feature areas to client methods and targeted protocol operations rather than mixing everything into one monolith.

7. **Sync and state reconciliation**
   - `lib/WAUSync/` and `lib/Utils/sync-action-utils.js` implement **server-side sync/query flows** for contacts, devices, status, disappearing mode, bot profile, and LID-related sync.
   - `lib/Utils/history.js`, `event-buffer.js`, and `baileys-event-stream.js` support **ordered event delivery** and history replay so the consumer can reconcile state after reconnects.

8. **Local state and persistence**
   - `lib/Store/` provides pluggable storage abstractions: **in-memory**, **cache-manager-backed**, and **ordered dictionary/object repository** implementations.
   - Auth persistence is separated into helpers like `use-single-file-auth-state`, `use-multi-file-auth-state`, and `use-mongo-file-auth-state`, which define the boundary for storing long-lived credentials outside the socket layer.

9. **Shared domain types**
   - `lib/Types/` centralizes models for **auth, chats, contacts, messages, groups, labels, calls, newsletters, sync state, and socket events**.
   - These are the shared contracts used across the protocol, socket, store, and utility layers.

10. **Support utilities**
   - `lib/Utils/` is a broad support layer for **JID resolution, logging, mutexes, browser/client metadata, LTC/token helpers, identity-change handling, and generic message utilities**.
   - These modules keep the core socket and protocol code focused on transport and state handling.

11. **Main runtime boundaries**
   - **External boundary:** WhatsApp Web servers over WebSocket.
   - **Security boundary:** Noise + Signal cryptography before meaningful message exchange.
   - **Persistence boundary:** optional filesystem or MongoDB-backed auth/state storage.
   - **Consumer boundary:** application code interacts with the socket API, event stream, and store abstractions exported from `lib/index.js`.

12. **How the pieces fit together**
   - Outbound: consumer API call → feature module (`messages-send`, `groups`, etc.) → protocol encoding (`WAProto`/`WABinary`) → encrypted transport (`Noise`/`Signal`) → WebSocket.
   - Inbound: WebSocket frame → binary decode → protocol/message processing → event buffering and sync helpers → store updates and emitted events for the consumer.

Main entry points: [`lib/Socket/socket.js`](https://github.com/this-xys/baileys/blob/main/lib/Socket/socket.js) (Socket Core), [`lib/Utils/index.js`](https://github.com/this-xys/baileys/blob/main/lib/Utils/index.js) (Utils), [`lib/index.js`](https://github.com/this-xys/baileys/blob/main/lib/index.js) (Public API), [`lib/Socket/messages-send.js`](https://github.com/this-xys/baileys/blob/main/lib/Socket/messages-send.js) (Send)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_g_protocol["Protocol"]
  node_waproto["WAProto<br/>schema<br/>[index.js]"]
  node_wabinary{{"WABinary<br/>codec<br/>[index.js]"}}
  node_wam{{"WAM<br/>wire model<br/>[index.js]"}}
end

subgraph group_g_connection["Connection"]
  node_lib_index["Public API<br/>entrypoint<br/>[index.js]"]
  node_socket_client["Client<br/>transport<br/>[index.js]"]
  node_socket_core["Socket Core<br/>session<br/>[socket.js]"]
  node_messages_send["Send<br/>pipeline<br/>[messages-send.js]"]
  node_messages_recv["Receive<br/>pipeline<br/>[messages-recv.js]"]
  node_feature_facades["Domain APIs<br/>facade set<br/>[chats.js]"]
end

subgraph group_g_security["Security"]
  node_signal{{"Signal<br/>crypto<br/>[libsignal.js]"}}
  node_noise{{"Noise<br/>handshake<br/>[noise-handler.js]"}}
end

subgraph group_g_state["State"]
  node_sync["USync<br/>[index.js]"]
  node_event_stream["Events<br/>stream"]
  node_store[("Store<br/>persistence<br/>[index.js]")]
  node_auth_state[("Auth State<br/>credential store")]
end

subgraph group_g_shared["Shared"]
  node_types["Types<br/>contracts<br/>[index.js]"]
  node_utils["Utils<br/>helpers<br/>[index.js]"]
  node_defaults["Defaults<br/>constants<br/>[index.js]"]
  node_engine_req["Engine Check<br/>runtime guard"]
end

node_lib_index -->|"exports"| node_socket_core
node_lib_index -->|"exports"| node_store
node_lib_index -->|"exports"| node_auth_state
node_lib_index -->|"exports"| node_types
node_lib_index -->|"exports"| node_utils
node_socket_core -->|"uses"| node_socket_client
node_socket_core -->|"routes outbound"| node_messages_send
node_socket_core -->|"routes inbound"| node_messages_recv
node_socket_core -->|"exposes"| node_feature_facades
node_messages_send -->|"encodes"| node_wabinary
node_messages_send -->|"builds"| node_waproto
node_messages_recv -->|"decodes"| node_wabinary
node_messages_recv -->|"processes"| node_utils
node_feature_facades -->|"queries"| node_sync
node_feature_facades -->|"dispatches"| node_messages_send
node_noise -->|"secures"| node_socket_client
node_noise -->|"derives keys"| node_signal
node_signal -->|"uses"| node_types
node_sync -->|"emits"| node_event_stream
node_event_stream -->|"updates"| node_store
node_auth_state -->|"persists via"| node_store
node_utils -->|"supports"| node_noise
node_utils -->|"supports"| node_signal
node_utils -->|"supports"| node_waproto
node_defaults -->|"feeds"| node_utils
node_engine_req -->|"guards"| node_socket_core

click node_lib_index "https://github.com/this-xys/baileys/blob/main/lib/index.js"
click node_waproto "https://github.com/this-xys/baileys/blob/main/WAProto/index.js"
click node_wabinary "https://github.com/this-xys/baileys/blob/main/lib/WABinary/index.js"
click node_wam "https://github.com/this-xys/baileys/blob/main/lib/WAM/index.js"
click node_socket_client "https://github.com/this-xys/baileys/blob/main/lib/Socket/Client/index.js"
click node_socket_core "https://github.com/this-xys/baileys/blob/main/lib/Socket/socket.js"
click node_messages_send "https://github.com/this-xys/baileys/blob/main/lib/Socket/messages-send.js"
click node_messages_recv "https://github.com/this-xys/baileys/blob/main/lib/Socket/messages-recv.js"
click node_feature_facades "https://github.com/this-xys/baileys/blob/main/lib/Socket/chats.js"
click node_signal "https://github.com/this-xys/baileys/blob/main/lib/Signal/libsignal.js"
click node_noise "https://github.com/this-xys/baileys/blob/main/lib/Utils/noise-handler.js"
click node_sync "https://github.com/this-xys/baileys/blob/main/lib/WAUSync/index.js"
click node_event_stream "https://github.com/this-xys/baileys/blob/main/lib/Utils/baileys-event-stream.js"
click node_store "https://github.com/this-xys/baileys/blob/main/lib/Store/index.js"
click node_auth_state "https://github.com/this-xys/baileys/blob/main/lib/Utils/use-single-file-auth-state.js"
click node_types "https://github.com/this-xys/baileys/blob/main/lib/Types/index.js"
click node_utils "https://github.com/this-xys/baileys/blob/main/lib/Utils/index.js"
click node_defaults "https://github.com/this-xys/baileys/blob/main/lib/Defaults/index.js"
click node_engine_req "https://github.com/this-xys/baileys/blob/main/engine-requirements.js"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_waproto,node_wabinary,node_wam toneBlue
class node_lib_index,node_socket_client,node_socket_core,node_messages_send,node_messages_recv,node_feature_facades toneAmber
class node_signal,node_noise toneMint
class node_sync,node_event_stream,node_store,node_auth_state toneRose
class node_types,node_utils,node_defaults,node_engine_req toneIndigo
```

## Components

### Protocol

Schema and wire-format layers for WhatsApp payloads.

- [WAProto](https://github.com/this-xys/baileys/blob/main/WAProto/index.js): schema · Generated protobuf schema and statics for WhatsApp message payloads. · `WAProto/index.js`
- [WABinary](https://github.com/this-xys/baileys/blob/main/lib/WABinary/index.js): codec · Binary protocol encoding and decoding for wire frames. · `lib/WABinary/index.js`
- [WAM](https://github.com/this-xys/baileys/blob/main/lib/WAM/index.js): wire model · Additional binary message encoding and binary info helpers. · `lib/WAM/index.js`

### Connection

Socket lifecycle, transport, and feature facades.

- [Public API](https://github.com/this-xys/baileys/blob/main/lib/index.js): entrypoint · Main export surface for socket, store, auth, types, and utilities. · `lib/index.js`
- [Client](https://github.com/this-xys/baileys/blob/main/lib/Socket/Client/index.js): transport · WebSocket transport adapter and client session glue. · `lib/Socket/Client/index.js`
- [Socket Core](https://github.com/this-xys/baileys/blob/main/lib/Socket/socket.js): session · Connection lifecycle, reconnects, auth setup, and event routing. · `lib/Socket/socket.js`
- [Send](https://github.com/this-xys/baileys/blob/main/lib/Socket/messages-send.js): pipeline · Outbound message construction and dispatch path. · `lib/Socket/messages-send.js`
- [Receive](https://github.com/this-xys/baileys/blob/main/lib/Socket/messages-recv.js): pipeline · Inbound message handling and dispatch path. · `lib/Socket/messages-recv.js`
- [Domain APIs](https://github.com/this-xys/baileys/blob/main/lib/Socket/chats.js): facade set · Feature-specific socket facades for chats, groups, communities, newsletter, business, mex, and usync. · `lib/Socket/chats.js`

### Security

Handshake, cryptography, and Signal session handling.

- [Signal](https://github.com/this-xys/baileys/blob/main/lib/Signal/libsignal.js): crypto · Signal protocol implementation for key exchange and message encryption. · `lib/Signal/libsignal.js`
- [Noise](https://github.com/this-xys/baileys/blob/main/lib/Utils/noise-handler.js): handshake · Noise-based session handshake and connection validation helpers. · `lib/Utils/noise-handler.js`

### State

Sync, event ordering, and persistence boundaries.

- [USync](https://github.com/this-xys/baileys/blob/main/lib/WAUSync/index.js): sync · Server-side sync/query flows for contacts, devices, status, and related state. · `lib/WAUSync/index.js`
- [Events](https://github.com/this-xys/baileys/blob/main/lib/Utils/baileys-event-stream.js): stream · Ordered event buffering, history replay, and delivery helpers. · `lib/Utils/baileys-event-stream.js`
- [Store](https://github.com/this-xys/baileys/blob/main/lib/Store/index.js): persistence · In-memory and cache-backed state repositories and ordered dictionaries. · `lib/Store/index.js`
- [Auth State](https://github.com/this-xys/baileys/blob/main/lib/Utils/use-single-file-auth-state.js): credential store · Single-file, multi-file, and Mongo-backed auth persistence helpers. · `lib/Utils/use-single-file-auth-state.js`

### Shared

Cross-cutting types and utility helpers.

- [Types](https://github.com/this-xys/baileys/blob/main/lib/Types/index.js): contracts · Shared domain models for auth, chats, contacts, messages, groups, labels, calls, newsletters, sync, and sockets. · `lib/Types/index.js`
- [Utils](https://github.com/this-xys/baileys/blob/main/lib/Utils/index.js): helpers · Shared helpers for message processing, JID resolution, media, logging, crypto, and token helpers. · `lib/Utils/index.js`
- [Defaults](https://github.com/this-xys/baileys/blob/main/lib/Defaults/index.js): constants · Default data such as phone number MCC mappings. · `lib/Defaults/index.js`
- [Engine Check](https://github.com/this-xys/baileys/blob/main/engine-requirements.js): runtime guard · Runtime compatibility requirements for startup validation. · `engine-requirements.js`

## Connections

- Public API → Socket Core: exports · Public API exposes the socket client surface.
- Public API → Store: exports · Public API exposes state store helpers.
- Public API → Auth State: exports · Public API exposes auth-state helpers.
- Public API → Types: exports · Public API re-exports shared contracts.
- Public API → Utils: exports · Public API re-exports utility helpers.
- Socket Core → Client: uses · Core socket relies on the WebSocket transport adapter.
- Socket Core → Send: routes outbound · Session layer delegates send operations.
- Socket Core → Receive: routes inbound · Session layer delegates receive processing.
- Socket Core → Domain APIs: exposes · Socket core surfaces domain-specific APIs.
- Send → WABinary: encodes · Outbound payloads are serialized into wire frames.
- Send → WAProto: builds · Message structures come from the protobuf schema.
- Receive → WABinary: decodes · Inbound frames are decoded from binary protocol.
- Receive → Utils: processes · Message normalization and interpretation use shared helpers.
- Domain APIs → USync: queries · Feature APIs invoke sync and targeted protocol operations.
- Domain APIs → Send: dispatches · Domain facades send actions through the message pipeline.
- Noise → Client: secures · Handshake and validation happen before and during transport.
- Noise → Signal: derives keys · Noise setup depends on Signal cryptographic state.
- Signal → Types: uses · Signal-related domain types are shared across layers.
- USync → Events: emits · Sync flows feed ordered event delivery.
- Events → Store: updates · Buffered events reconcile local state through stores.
- Auth State → Store: persists via · Auth helpers persist credentials outside the socket layer.
- Utils → Noise: supports · Utility modules implement handshake and validation helpers.
- Utils → Signal: supports · Utility modules include Signal-related helpers.
- Utils → WAProto: supports · Helpers assist in message decoding and processing.
- Defaults → Utils: feeds · Default metadata is consumed by utility helpers.
- Engine Check → Socket Core: guards · Runtime compatibility is checked during startup.
