Binary Protocol Overview
WhatsApp uses a custom binary protocol instead of plain XML to reduce bandwidth and improve performance. Messages are encoded as binary nodes with a compact token-based encoding system.Key Components
- Binary Nodes: Structured data units with tags, attributes, and content
- Token Dictionary: Predefined tokens for common strings
- Binary Encoding: Efficient byte-level encoding
- Compression: Optional frame compression
Binary Node Structure
Fromsrc/WABinary/types.ts:9-13:
Components
Tag: Identifies the node typeToken-Based Encoding
To reduce message size, WhatsApp uses a dictionary of predefined tokens for common strings.Token Types
Fromsrc/WABinary/constants.ts:1-19:
Single-Byte Tokens
Common strings encoded as single bytes (0-235):"type" is encoded as byte 4 instead of 4 bytes.
Double-Byte Tokens
Less common strings use two bytes (dictionary index + token index):Encoding Process
Fromsrc/WABinary/encode.ts:5-12:
Encoding Steps
- Write list header - Number of elements (tag + attributes + content)
- Write tag - Encoded as token or raw string
- Write attributes - Each key-value pair encoded
- Write content - Recursively encode based on type
String Encoding
Fromsrc/WABinary/encode.ts:179-209:
Packed Encoding
Nibble Packing: Stores numbers and special chars (-, .) as 4-bit valuesJID Encoding
WhatsApp IDs (JIDs) are specially encoded:Decoding Process
Fromsrc/WABinary/decode.ts:9-18:
Decoding Steps
- Decompress - If compression flag is set
- Read list size - Number of elements
- Read tag - First element is always the tag
- Read attributes - Pairs of key-value strings
- Read content - If list size is even, read content
src/WABinary/decode.ts:251-296:
Frame Format
WhatsApp frames are wrapped with:Header
Fromsrc/Defaults/index.ts:34:
Compression
Frames can be optionally compressed using zlib:Common Node Examples
Query Node
Message Node
Presence Node
Working with Binary Nodes
Creating Nodes
Parsing Nodes
Extracting Content
Converting to String (Debug)
Protocol Constants
Fromsrc/Defaults/index.ts:
Best Practices
Use Helper Functions: Baileys provides utilities for working with binary nodes. Use them instead of manual parsing.
Debugging Tools
Enable trace logging to see binary node XML representations:Learn More
To understand the underlying cryptography:- Libsignal Protocol: End-to-end encryption protocol
- Noise Protocol Framework: Handshake patterns and session keys
Next Steps
- Custom Functionality - Using binary nodes in handlers
- WebSocket Events - Intercepting protocol messages
- Debugging - Troubleshooting protocol issues