Enable debug logging
The first step when building custom functionality is to see what WhatsApp is actually sending. Set the logger to'debug' level and every raw frame that arrives over the WebSocket will be printed to the console:
How WhatsApp communicates: binary nodes
Every message WhatsApp sends arrives as a binary node with three components:
The
BinaryNode type gives you full TypeScript coverage when working with these frames:
Register WebSocket callbacks
Once you have identified the frame you want to handle in the debug output, register a callback usingsock.ws.on. The event name uses a CB: prefix followed by a filter expression that Baileys evaluates against each incoming node.
Match by tag
Match by tag and attribute value
Match by tag, attribute value, and content node tag
CB:<tag>— match any node with this tagCB:<tag>,<attr>:<value>— also require an attribute to equal a specific valueCB:<tag>,<attr>:<value>,<content-tag>— also require the first content child to have this tag
BinaryNode object, so you can inspect node.attrs, node.content, and any nested children from a single handler.
Typed handler example
Understanding the event flow
When a frame arrives, Baileys decodes the binary payload, runs it through the registeredCB: callbacks, and then — for frames it recognizes — emits the corresponding high-level event on sock.ev. Your custom callbacks run at the raw frame layer, before the high-level event is emitted, which means you can handle protocol messages that Baileys does not yet surface as events.