Receive WhatsApp messages in realtime over WebSocket
Updated
Short answer: Connect a Socket.IO v4 client to wss://{name}.eai.menustudioai.com with transports set to websocket and your instance token as the apikey query parameter, then listen for messages.upsert to receive new WhatsApp messages as they arrive.
Rules of the stream
- One socket per instance; you only receive the events of the instance whose token you pass.
- WebSocket transport only: HTTP long-polling is rejected, so set transports: ["websocket"].
- Receive-only: events you emit are dropped. Send messages with the REST endpoints.
Node.js example
import { io } from 'socket.io-client';
const socket = io('wss://{name}.eai.menustudioai.com', {
path: '/socket.io',
transports: ['websocket'],
query: { apikey: process.env.WEAI_TOKEN },
});
socket.on('messages.upsert', (evt) => {
const msg = evt.data;
if (msg.key.fromMe) return;
console.log(msg.key.remoteJid, msg.message?.conversation);
});
socket.on('connection.update', (evt) => console.log('connection', evt.data));Most useful events
- messages.upsert: a new message was received or sent.
- messages.update: delivery / read status changed.
- connection.update: the phone connected or disconnected.
- qrcode.updated: a new QR code is available while linking.
- presence.update, chats.upsert, contacts.upsert, group-participants.update and more.
To build an auto-reply bot, handle messages.upsert, skip events where key.fromMe is true, and answer with POST /message/sendText.
Your phone. Your API. From 5.50 €/instance a month.