Skip to content

Commit 92626fa

Browse files
fix(baileys): resolve incoming message events not working after reconnection
- 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
1 parent 3454bec commit 92626fa

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

src/api/integrations/channel/whatsapp/baileysMessage.processor.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,22 @@ export class BaileysMessageProcessor {
1919
}>();
2020

2121
mount({ onMessageReceive }: MountProps) {
22+
// Se já existe subscription, fazer cleanup primeiro
23+
if (this.subscription && !this.subscription.closed) {
24+
this.subscription.unsubscribe();
25+
}
26+
27+
// Se o Subject foi completado, recriar
28+
if (this.messageSubject.closed) {
29+
this.processorLogs.warn('MessageSubject was closed, recreating...');
30+
this.messageSubject = new Subject<{
31+
messages: WAMessage[];
32+
type: MessageUpsertType;
33+
requestId?: string;
34+
settings: any;
35+
}>();
36+
}
37+
2238
this.subscription = this.messageSubject
2339
.pipe(
2440
tap(({ messages }) => {

src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,11 @@ export class BaileysStartupService extends ChannelStartupService {
710710
this.loadWebhook();
711711
this.loadProxy();
712712

713+
// Remontar o messageProcessor para garantir que está funcionando após reconexão
714+
this.messageProcessor.mount({
715+
onMessageReceive: this.messageHandle['messages.upsert'].bind(this),
716+
});
717+
713718
return await this.createClient(number);
714719
} catch (error) {
715720
this.logger.error(error);

0 commit comments

Comments
 (0)