Overview
makeWASocket is the main factory function for creating a new WhatsApp Web socket connection. It initializes and returns a WASocket instance that provides access to all WhatsApp functionality.
Function signature
function makeWASocket ( config : UserFacingSocketConfig ) : WASocket
Parameters
config
UserFacingSocketConfig
required
Configuration object for the socket connection. See SocketConfig for all available options. Show UserFacingSocketConfig definition
type UserFacingSocketConfig = Partial < SocketConfig > & {
auth : AuthenticationState
}
All SocketConfig properties are optional except auth, which must be provided.
Required configuration
The only required property is auth, which manages authentication state:
config.auth
AuthenticationState
required
Authentication state object containing credentials and keys. See AuthenticationState for details.
Return value
Returns a socket instance with methods and properties for interacting with WhatsApp. The socket is a layered object combining functionality from multiple modules: Show Core socket properties
Socket type identifier (multi-device)
Underlying WebSocket connection
Event emitter for socket events. Subscribe to events like connection.update, messages.upsert, chats.update, etc.
Current authentication state with creds and keys
Information about the authenticated user
Logout from the current session
Close the socket connection
waitForConnectionUpdate
(check: (u: Partial<ConnectionState>) => boolean, timeoutMs?: number) => Promise<Partial<ConnectionState>>
Wait for a specific connection state update
Wait until the WebSocket connection is open
Send a raw message payload
getMessage
(key: WAMessageKey) => Promise<proto.IMessage | undefined>
Fetch a message from the store
groupMetadata
(jid: string) => Promise<GroupMetadata>
Fetch metadata for a group
Add/remove participants from a group
Fetch metadata for a community
The socket includes many more methods for handling messages, chats, presence, business features, and more.
Basic usage
import makeWASocket , { useMultiFileAuthState } from '@whiskeysockets/baileys'
// Load or create authentication state
const { state , saveCreds } = await useMultiFileAuthState ( 'auth_info' )
// Create socket connection
const sock = makeWASocket ({
auth: state ,
printQRInTerminal: true
})
// Listen for connection updates
sock . ev . on ( 'connection.update' , ( update ) => {
const { connection , lastDisconnect } = update
if ( connection === 'close' ) {
console . log ( 'Connection closed' )
} else if ( connection === 'open' ) {
console . log ( 'Connection opened' )
}
})
// Save credentials when updated
sock . ev . on ( 'creds.update' , saveCreds )
Usage with custom configuration
import makeWASocket , {
DisconnectReason ,
useMultiFileAuthState
} from '@whiskeysockets/baileys'
import pino from 'pino'
const { state , saveCreds } = await useMultiFileAuthState ( 'auth_info' )
const sock = makeWASocket ({
auth: state ,
logger: pino ({ level: 'info' }),
printQRInTerminal: true ,
// Browser metadata
browser: [ 'MyApp' , 'Chrome' , '1.0.0' ],
// Custom timeouts
connectTimeoutMs: 60000 ,
defaultQueryTimeoutMs: 60000 ,
// Mark as online when connected
markOnlineOnConnect: true ,
// Sync full chat history
syncFullHistory: false ,
// Message handling
getMessage : async ( key ) => {
// Retrieve message from your database
return await getMessageFromDB ( key )
}
})
sock . ev . on ( 'creds.update' , saveCreds )
sock . ev . on ( 'connection.update' , ( update ) => {
const { connection , lastDisconnect } = update
if ( connection === 'close' ) {
const shouldReconnect =
lastDisconnect ?. error ?. output ?. statusCode !== DisconnectReason . loggedOut
if ( shouldReconnect ) {
// Reconnect if not logged out
makeWASocket ( config )
}
}
})
// Listen for new messages
sock . ev . on ( 'messages.upsert' , async ({ messages }) => {
for ( const msg of messages ) {
console . log ( 'Received message:' , msg )
}
})
Event handling
The socket’s ev property is an event emitter that fires various events:
Connection Events
Message Events
Chat Events
Group Events
sock . ev . on ( 'connection.update' , ( update ) => {
console . log ( 'Connection status:' , update . connection )
})
The socket merges defaults from DEFAULT_CONNECTION_CONFIG with your provided configuration. Only the auth property is required; all other settings have sensible defaults.
See also