Configuration
Configure WebSocket through environment variables:.env
Host Restrictions
Control which hosts can connect to your WebSocket server:- Localhost Only
- Specific IPs
- Allow All
Default configuration for local development:
.env
Connection Modes
WebSocket supports two connection modes:- Per-Instance Namespace
- Global Events
Connect to a specific instance namespace:Receives events only from that instance.
Authentication
WebSocket connections require authentication via API key:1
Query Parameter
Pass API key in connection URL:
2
Header (Alternative)
Or pass as custom header:
3
Validation
Evolution API validates the API key:
- For instance namespaces: Instance API key
- For global connection: Global API key
Connection Examples
Available Events
You can listen for any WhatsApp event through WebSocket:Instance Events
qrcode.updated- QR code changesconnection.update- Connection status changes
Message Events
messages.set- Message history loadedmessages.upsert- New messages received/sentmessages.edited- Messages editedmessages.update- Message status updates (read, delivered)messages.delete- Messages deletedsend.message- Message sent confirmationsend.message.update- Sent message status update
Contact Events
contacts.set- Contacts loadedcontacts.upsert- Contact added/updatedcontacts.update- Contact information changedpresence.update- Contact online/offline status
Chat Events
chats.set- Chats loadedchats.upsert- New chat createdchats.update- Chat updatedchats.delete- Chat deleted
Group Events
groups.upsert- Group created/joinedgroup.update- Group information changedgroup.participants.update- Participants added/removed
Other Events
labels.edit- Label editedlabels.association- Label associated with chatcall- Incoming call
Per-Instance Configuration
Configure which events are emitted for a specific instance:Event Payload Structure
All WebSocket events have a consistent structure:Sending Custom Commands
Send custom commands to WhatsApp through WebSocket:Best Practices
1
Handle Reconnections
Always enable automatic reconnection:
2
Use Event Filtering
Configure only needed events to reduce bandwidth:
3
Implement Error Handling
Handle all error events:
4
Secure Your Connection
- Use HTTPS/WSS in production
- Restrict
WEBSOCKET_ALLOWED_HOSTS - Never expose API keys in client-side code
5
Monitor Connection State
Track connection status in your UI:
CORS Configuration
WebSocket respects your CORS settings:.env
WebSocket CORS is automatically configured based on your
CORS_ORIGIN setting.Troubleshooting
Connection rejected
Connection rejected
- Verify API key is correct
- Check that WebSocket is enabled:
WEBSOCKET_ENABLED=true - Ensure your IP is in
WEBSOCKET_ALLOWED_HOSTS - Check Evolution API logs for authentication errors
No events received
No events received
- Verify instance is connected to WhatsApp
- Check that specific events are enabled in instance configuration
- Ensure you’re connected to correct namespace
- Enable logging to see if events are being emitted
Frequent disconnections
Frequent disconnections
- Check network stability
- Increase
reconnectionDelayin client configuration - Monitor server resources (CPU, memory)
- Check for firewall issues blocking WebSocket connections
Authentication failed
Authentication failed
- Verify API key format (no extra spaces)
- For instance namespaces, use instance API key
- For global events, use global API key
- Check that instance exists and is active
CORS errors in browser
CORS errors in browser
- Add your domain to
CORS_ORIGIN - Restart Evolution API after changing CORS settings
- Check browser console for specific CORS error
- Ensure protocol matches (http/https)