Skip to main content

Overview

makeWASocket is the primary function used to create a WhatsApp Web socket connection. It accepts a configuration object and returns a socket instance with all methods for interacting with the WhatsApp Web API.

Function Signature

const makeWASocket = (config: UserFacingSocketConfig) => WASocket

Parameters

config
UserFacingSocketConfig
required
Configuration object for the socket connection. This is a combination of Partial<SocketConfig> with a required auth property.

Return Value

sock
WASocket
Returns a socket instance with all methods for interacting with the WhatsApp Web API, including:
  • Message sending and receiving
  • Group management
  • Contact management
  • Media handling
  • Event listeners
  • And more

Basic Example

import makeWASocket, { useMultiFileAuthState } from '@whiskeysockets/baileys'

const { state, saveCreds } = await useMultiFileAuthState('auth_info_baileys')

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)

Advanced Example with Caching

import makeWASocket, { 
  useMultiFileAuthState,
  makeCacheableSignalKeyStore,
  fetchLatestBaileysVersion 
} from '@whiskeysockets/baileys'
import NodeCache from '@cacheable/node-cache'
import P from 'pino'

const logger = P({ level: 'trace' })
const msgRetryCounterCache = new NodeCache()

const { state, saveCreds } = await useMultiFileAuthState('baileys_auth_info')
const { version } = await fetchLatestBaileysVersion()

const sock = makeWASocket({
  version,
  logger,
  auth: {
    creds: state.creds,
    keys: makeCacheableSignalKeyStore(state.keys, logger),
  },
  msgRetryCounterCache,
  generateHighQualityLinkPreview: true,
  getMessage: async (key) => {
    // Implement message retrieval from your store
    return undefined
  }
})

sock.ev.on('creds.update', saveCreds)

Pairing Code Example

import makeWASocket, { useMultiFileAuthState } from '@whiskeysockets/baileys'

const { state, saveCreds } = await useMultiFileAuthState('auth_info')

const sock = makeWASocket({
  auth: state,
  printQRInTerminal: false // Must be false for pairing code
})

if (!sock.authState.creds.registered) {
  const phoneNumber = '1234567890' // Without + or () or -
  const code = await sock.requestPairingCode(phoneNumber)
  console.log(`Pairing code: ${code}`)
}

sock.ev.on('creds.update', saveCreds)

Implementation Details

The makeWASocket function internally:
  1. Merges the provided config with DEFAULT_CONNECTION_CONFIG
  2. Creates a socket through the makeCommunitiesSocket layer
  3. Returns the fully configured socket instance
const makeWASocket = (config: UserFacingSocketConfig) => {
  const newConfig = {
    ...DEFAULT_CONNECTION_CONFIG,
    ...config
  }
  return makeCommunitiesSocket(newConfig)
}

See Also

  • SocketConfig - Complete socket configuration reference
  • Browsers - Browser configuration options
  • Events - Available socket events