-
Notifications
You must be signed in to change notification settings - Fork 5.1k
fix(baileys): resolve incoming message events not working after reconnection #2186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(baileys): resolve incoming message events not working after reconnection #2186
Conversation
…nection - Add cleanup logic in mount() to prevent memory leaks from multiple subscriptions - Recreate messageSubject if it was completed during logout - Remount messageProcessor in connectToWhatsapp() to ensure subscription is active after reconnection This fixes the issue where incoming message events stop working after logout and reconnect, while outgoing message events continue to work normally. The root cause was that onDestroy() calls complete() on the RxJS Subject, making it permanently closed. When reconnecting, the Subject would silently ignore all new messages. The fix ensures that: 1. Old subscriptions are properly cleaned up before creating new ones 2. If the Subject is closed, a new one is created automatically 3. The messageProcessor is remounted on every connection to ensure active subscription
Reviewer's GuideThis PR adds defensive lifecycle management to the BaileysMessageProcessor by cleaning up old subscriptions and auto-recovering a completed RxJS Subject in mount(), and ensures the processor is remounted on every manual reconnection in connectToWhatsapp(). Sequence diagram for incoming message event handling after reconnectionsequenceDiagram
participant Client
participant "BaileysStartupService"
participant "BaileysMessageProcessor"
participant "Webhook"
Client->>"BaileysStartupService": POST /instance/connect
"BaileysStartupService"->>"BaileysMessageProcessor": mount({ onMessageReceive })
"BaileysMessageProcessor"->>"BaileysMessageProcessor": Cleanup old subscription
"BaileysMessageProcessor"->>"BaileysMessageProcessor": Recreate Subject if completed
Client->>"BaileysStartupService": Incoming WhatsApp message
"BaileysStartupService"->>"BaileysMessageProcessor": processMessage()
"BaileysMessageProcessor"->>"Webhook": Trigger webhook for incoming message
Class diagram for updated BaileysMessageProcessor lifecycleclassDiagram
class BaileysMessageProcessor {
- messageSubject: Subject
- subscription: Subscription
+ mount(props: MountProps)
+ onDestroy()
}
BaileysMessageProcessor : mount() cleans up old subscription
BaileysMessageProcessor : mount() recreates Subject if completed
BaileysMessageProcessor : mount() sets up new subscription
Class diagram for BaileysStartupService changesclassDiagram
class BaileysStartupService {
+ connectToWhatsapp(number?: string): Promise<WASocket>
- messageProcessor: BaileysMessageProcessor
- messageHandle: any
}
BaileysStartupService : connectToWhatsapp() calls messageProcessor.mount() on every connection
File-Level Changes
Possibly linked issues
Tips and commandsInteracting with Sourcery
Customizing Your ExperienceAccess your dashboard to:
Getting Help
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hey there - I've reviewed your changes - here's some feedback:
- Consider moving the messageProcessor.mount() call to after the Baileys socket emits its open event, so you don’t miss any messages that arrive during the initial client handshake.
- Simplify the Subject lifecycle by always recreating messageSubject at the start of mount() (after unsubscribing) rather than conditionally checking closed, which makes the reset behavior more explicit.
- Add a debug log when unsubscribing the old subscription to help trace cleanup operations and ensure no dangling subscriptions remain after reconnect.
Prompt for AI Agents
Please address the comments from this code review:
## Overall Comments
- Consider moving the messageProcessor.mount() call to after the Baileys socket emits its open event, so you don’t miss any messages that arrive during the initial client handshake.
- Simplify the Subject lifecycle by always recreating messageSubject at the start of mount() (after unsubscribing) rather than conditionally checking closed, which makes the reset behavior more explicit.
- Add a debug log when unsubscribing the old subscription to help trace cleanup operations and ensure no dangling subscriptions remain after reconnect.Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.
|
Aprovação necessária, o app está quebrando exatamente nessa parte |

Fix: Incoming message events not working after reconnection
🐛 Problem Description
After disconnecting an instance (using
/instance/logout) and reconnecting (using/instance/connect), incoming message events stop being triggered, while outgoing message events continue to work normally.Affected Scenarios:
/instance/restart) - Works correctly/instance/logout→/instance/connect) - BrokenUser Impact:
🔍 Root Cause Analysis
The issue is in the
BaileysMessageProcessorlifecycle management:messageProcessor.mount()is called in the constructor, creating an RxJS Subject and subscription/instance/logout→logoutInstance()→messageProcessor.onDestroy()→messageSubject.complete()complete()is called on an RxJS Subject, it becomes permanently closed and silently ignores all futurenext()calls/instance/connect→connectToWhatsapp()→createClient()→ event handlers are set upevents['messages.upsert']→messageProcessor.processMessage()→messageSubject.next()→ SILENTLY IGNORED (Subject is completed)Why outgoing messages work:
Outgoing messages bypass the
messageProcessorentirely and callsendDataWebhook()directly.Why restart works:
The
restartInstance()method closes the WebSocket and recreates the client WITHOUT callinglogoutInstance(), soonDestroy()is never called and the messageProcessor remains functional.✅ Solution
This PR implements a defensive approach with auto-recovery:
Changes Made:
1. baileysMessage.processor.ts - Add cleanup and auto-recovery in
mount()Benefits:
2. whatsapp.baileys.service.ts - Remount processor on connection
Benefits:
mount()🧪 Testing
Manual Testing Steps:
POST /instance/createPOST /instance/connect/{instanceName}DELETE /instance/logout/{instanceName}POST /instance/connect/{instanceName}Tested Scenarios:
📊 Performance Impact
🔄 Backward Compatibility
📝 Additional Notes
complete()is called elsewhere in the future, the system auto-recovers🔗 Related Issues
This fix resolves the issue where users reported that after disconnecting and reconnecting instances, they stop receiving incoming message notifications/events, while outgoing messages continue to work normally.
Checklist:
Summary by Sourcery
Fix incoming message events not being handled after manual logout and reconnect by adding cleanup and auto-recovery in the message processor and remounting it on each connection
Bug Fixes: