Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@
private isDeleting = false; // Flag to prevent reconnection during deletion
private logBaileys = this.configService.get<Log>('LOG').BAILEYS;
private eventProcessingQueue: Promise<void> = Promise.resolve();
private _lastStream515At = 0;

// Cumulative history sync counters (reset on new sync or completion)
private historySyncMessageCount = 0;
Expand All @@ -275,6 +276,13 @@
private readonly MESSAGE_CACHE_TTL_SECONDS = 5 * 60; // 5 minutes - avoid duplicate message processing
private readonly UPDATE_CACHE_TTL_SECONDS = 30 * 60; // 30 minutes - avoid duplicate status updates

// Reconnect behaviour for the Baileys "stream:error 515" sequence.
// After WhatsApp emits 515 it usually closes with `loggedOut`; that close is *not* a real logout
// and we should reconnect. We treat any close arriving within this grace window as 515-driven.
private static readonly STREAM_515_RECONNECT_GRACE_MS = 30_000;
// The numeric WhatsApp stream-error code that triggers the grace-period reconnect above.
private static readonly STREAM_ERROR_CODE_RECONNECT = '515';

public stateConnection: wa.StateConnection = { state: 'close' };

public phoneNumber: string;
Expand Down Expand Up @@ -506,7 +514,12 @@
return;
}

const shouldReconnect = !codesToNotReconnect.includes(statusCode);
// If a stream:error 515 (Baileys' "restart needed" handshake) just fired,
// a follow-up loggedOut is the expected restart signal — not an actual
// logout — so reconnect anyway.
const recentStream515 = Date.now() - this._lastStream515At < BaileysStartupService.STREAM_515_RECONNECT_GRACE_MS;
const shouldReconnect =
!codesToNotReconnect.includes(statusCode) || (statusCode === DisconnectReason.loggedOut && recentStream515);

this.logger.info({
message: 'Connection closed, evaluating reconnection',
Expand All @@ -515,6 +528,7 @@
instanceName: this.instance.name,
});


Check failure on line 531 in src/api/integrations/channel/whatsapp/whatsapp.baileys.service.ts

View workflow job for this annotation

GitHub Actions / check-lint-and-build

Delete `⏎`
if (shouldReconnect) {
// Add 3 second delay before reconnection to prevent rapid reconnection loops
this.logger.info('Reconnecting in 3 seconds...');
Expand Down Expand Up @@ -813,6 +827,12 @@
this.sendDataWebhook(Events.CALL, payload, true, ['websocket']);
});

this.client.ws.on('CB:stream:error', (node: { attrs?: { code?: string | number } }) => {
if (String(node?.attrs?.code) === BaileysStartupService.STREAM_ERROR_CODE_RECONNECT) {
this._lastStream515At = Date.now();
}
});

this.phoneNumber = number;

return this.client;
Expand Down
Loading