Skip to content

Free, Open Source, Self-Hosted WhatsApp API Gateway

fix(baileys): deliver the messages WhatsApp queued during a disconnect

Yudhi Armyndharis4cee7d45ae9d
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 889862535..163e59641 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -18,6 +18,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed
+- Baileys sessions no longer drop the inbound messages WhatsApp queued while they were down. The offline replay is tagged `append`, which the upsert handler treated as history and skipped for anything older than the reconnect, so every message sent during an outage was never stored and never dispatched ([#1660](https://github.com/rmyndharis/OpenWA/issues/1660)). Thanks @fransarni for the report.
- Stopping, unlinking or force-killing a session announces `session.status: disconnected` after the engine is released, not while it is still tearing down, so a dashboard tab or a webhook consumer no longer learns the session is down in the one moment the API still reports its engine as loaded. On whatsapp-web.js that window lasted as long as Chromium took to close, and left an open QR modal on a dead code with the started actions still offered ([#1649](https://github.com/rmyndharis/OpenWA/issues/1649)).
- The dashboard blanks a session's displayed QR code as soon as the session disconnects, so a code minted by a connection that is gone is never left on screen to be scanned ([#1649](https://github.com/rmyndharis/OpenWA/issues/1649)).
- A pairing-code request whose retry budget runs out on a reloading WhatsApp Web page answers `503` instead of `500`, so a client can tell a retryable transport failure from a broken gateway ([#1654](https://github.com/rmyndharis/OpenWA/issues/1654)).
diff --git a/src/engine/adapters/baileys-events.spec.ts b/src/engine/adapters/baileys-events.spec.ts
index ed3f5afd8..55326f251 100644
--- a/src/engine/adapters/baileys-events.spec.ts
+++ b/src/engine/adapters/baileys-events.spec.ts
@@ -95,9 +95,6 @@ function makeHost(overrides: Partial<BaileysEventsHost> = {}): BaileysEventsHost
getFetchDispatcher: () => undefined,
toNeutralJid: (jid: string) => jid,
normalizedSelfJid: () => '[email protected]',
- // connectedAt only gates handleMessagesUpsert's history-replay skip; mapMessage itself never
- // reads it.
- connectedAt: 0,
inboundLimiter: new ConcurrencyLimiter(inboundMediaConcurrency()),
recordKeyLidMappings: noop,
recordMessage: noop,
diff --git a/src/engine/adapters/baileys-events.ts b/src/engine/adapters/baileys-events.ts
index c76beb21c..0cc09403d 100644
--- a/src/engine/adapters/baileys-events.ts
+++ b/src/engine/adapters/baileys-events.ts
@@ -102,8 +102,6 @@ export interface BaileysEventsHost {
loadLib(): Promise<typeof BaileysLib>;
/** Session proxy dispatcher for the media download; undefined = direct. */
getFetchDispatcher(): Dispatcher | undefined;
- /** Unix-seconds timestamp of the last 'open' connection.update — the live-vs-history discriminator. */
- readonly connectedAt: number;
/** The adapter's inbound media download gate (shared so the bound holds across all inbound paths). */
readonly inboundLimiter: ConcurrencyLimiter;
/** Learn any lid->pn pair a message key carries (also writes through to the persistent table). */
@@ -156,24 +154,19 @@ export class BaileysEvents {
if (!msg.message || !msg.key?.remoteJid) {
continue; // protocol/empty messages carry no neutral content
}
- if (event.type !== 'notify') {
- // Baileys echoes back OUR OWN just-sent messages through this same 'append' path too, and
- // sendContent() already emits onMessageCreate for those via emitOwnSendEcho() — always
- // exclude fromMe here (unconditionally, regardless of timestamp) so that echo doesn't fire
- // onMessageCreate a second time.
- if (msg.key.fromMe === true) {
- continue;
- }
- // For everyone else: gate on the message's own timestamp vs. this connection's open time,
- // not the upsert batch's `type` tag. `type: 'append'` usually means real history-sync
- // backfill, but Baileys can also tag a genuinely new CUSTOMER message 'append' when it
- // arrives in the same window as a reconnect's state-sync handshake — a strict
- // `type !== 'notify'` filter silently drops that message (observed as "the first message
- // after a reconnect gets ignored"). A message sent AFTER this connection opened is live
- // regardless of which tag the batch carries; true backfill always predates it.
- if (toUnixSeconds(msg.messageTimestamp) < this.host.connectedAt) {
- continue;
- }
+ // Baileys echoes back OUR OWN just-sent messages through this same 'append' path, and
+ // sendContent() already emits onMessageCreate for those via emitOwnSendEcho() — exclude
+ // fromMe on a non-notify batch so that echo doesn't fire onMessageCreate a second time.
+ //
+ // Everything else on an 'append' batch is live traffic, whatever its timestamp says. The tag
+ // marks WhatsApp's offline queue (`node.attrs.offline ? 'append' : 'notify'` in Baileys'
+ // messages-recv), i.e. the messages that arrived while this session was down, which by
+ // definition predate the reconnect. Real history never reaches this handler: it arrives on
+ // messaging-history.set and is captured dispatch-free. Re-delivery is harmless because the
+ // insert oracle dedupes on the WhatsApp message id, so a message already stored is not
+ // dispatched twice.
+ if (event.type !== 'notify' && msg.key.fromMe === true) {
+ continue;
}
// Throttle through the limiter so a burst of media messages can't run unbounded parallel
// downloads (each a full decrypted buffer in heap). Ordering stays correct — the message store
diff --git a/src/engine/adapters/baileys-lifecycle.ts b/src/engine/adapters/baileys-lifecycle.ts
index 5a9822518..0af76cac3 100644
--- a/src/engine/adapters/baileys-lifecycle.ts
+++ b/src/engine/adapters/baileys-lifecycle.ts
@@ -116,7 +116,7 @@ export function createProxyAgent(proxyUrl: string, connectTimeoutMs = PROXY_CONN
* behind them (sock, status, reconnect counters, the lazily-loaded library). The adapter keeps the
* public IWhatsAppEngine members as thin forwarders and injects this narrow host surface via
* closures, so the delegate never touches adapter state directly; the two state fields the rest of
- * the adapter reads live (`sock`, `connectedAt`) are public here and aliased by adapter accessors.
+ * the adapter reads live (`sock`) are public here and aliased by adapter accessors.
*/
export interface BaileysLifecycleHost {
readonly logger: ReturnType<typeof createLogger>;
@@ -174,10 +174,6 @@ export class BaileysLifecycle {
/** Live Baileys socket, null when disconnected. Public so the adapter's `sock` accessor can alias
* it (an unmodified spec pokes `adapter.sock` through a cast; delegate hosts read it live). */
sock: WASocket | null = null;
- /** Unix-seconds timestamp of the last 'open' connection.update, used to distinguish a genuinely
- * live message misfiled as 'append' (see BaileysEvents.handleMessagesUpsert) from real history backfill.
- * Public so the adapter can alias it for the events delegate's live read. */
- connectedAt = 0;
private status: EngineStatus = EngineStatus.DISCONNECTED;
private qrCode: string | null = null;
private phoneNumber: string | null = null;
@@ -508,10 +504,6 @@ export class BaileysLifecycle {
this.pushName = this.sock?.user?.name ?? null;
// I4: reset the reconnect counter on a successful connection.
this.reconnectAttempts = 0;
- // Small backward buffer for clock skew between this host and WhatsApp's server (messageTimestamp
- // is WA's clock, Date.now() is ours) — without it, a message sent right at reconnect time could
- // land a couple seconds "before" connectedAt and be misjudged as history.
- this.connectedAt = Math.floor(Date.now() / 1000) - 10;
this.setStatus(EngineStatus.READY);
this.host.getOnReady()?.(this.phoneNumber ?? '', this.pushName ?? '');
// WhatsApp only PUSHES a timelock when it changes, so a gateway that starts (or reconnects)
diff --git a/src/engine/adapters/baileys.adapter.spec.ts b/src/engine/adapters/baileys.adapter.spec.ts
index 382fb062a..5987a0f56 100644
--- a/src/engine/adapters/baileys.adapter.spec.ts
+++ b/src/engine/adapters/baileys.adapter.spec.ts
@@ -2254,21 +2254,51 @@ describe('BaileysAdapter inbound fan-out', () => {
expect(onMessage).not.toHaveBeenCalled();
});
- it('ignores an append upsert with no/old timestamp (real history backfill)', async () => {
+ // WhatsApp replays what it queued while the session was down, and Baileys tags that batch
+ // 'append' (messages-recv.js: `node.attrs.offline ? 'append' : 'notify'`). Those messages
+ // necessarily predate the reconnect, so a timestamp gate drops exactly the traffic an operator
+ // most needs. Real history arrives on messaging-history.set instead, which never dispatches.
+ it('processes an append upsert that predates the reconnect (WhatsApp offline queue)', async () => {
const onMessage = jest.fn();
const adapter = newAdapter();
await adapter.initialize({ onMessage });
- fakeSock.fire('connection.update', { connection: 'open' }); // sets connectedAt
+ fakeSock.fire('connection.update', { connection: 'open' });
fakeSock.fire('messages.upsert', {
type: 'append',
messages: [
{
- key: { remoteJid: '[email protected]', fromMe: false, id: 'OLD' },
- message: { conversation: 'old' },
- messageTimestamp: Math.floor(Date.now() / 1000) - 3600, // an hour before connectedAt
+ key: { remoteJid: '[email protected]', fromMe: false, id: 'QUEUED_WHILE_DOWN' },
+ message: { conversation: 'sent while the gateway was down' },
+ messageTimestamp: Math.floor(Date.now() / 1000) - 3600, // an hour before the reconnect
+ },
+ ],
+ });
+ await new Promise(r => setImmediate(r));
+ expect(onMessage).toHaveBeenCalledTimes(1);
+ });
+
+ // The invariant the offline-queue case above rests on: real history is bulk, arrives on its own
+ // event, and is handed over dispatch-free. Nothing here reaches the message webhook.
+ it('never dispatches bulk history as an inbound message', async () => {
+ const onMessage = jest.fn();
+ const onHistoryMessages = jest.fn();
+ const adapter = newAdapter();
+ await adapter.initialize({ onMessage, onHistoryMessages });
+ fakeSock.fire('connection.update', { connection: 'open' });
+ fakeSock.fire('messaging-history.set', {
+ contacts: [],
+ chats: [],
+ messages: [
+ {
+ key: { remoteJid: '[email protected]', fromMe: false, id: 'HIST' },
+ message: { conversation: 'from the history sync' },
+ messageTimestamp: 1700000000,
},
],
});
+ await new Promise(r => setImmediate(r));
+ await new Promise(r => setImmediate(r));
+ expect(onHistoryMessages).toHaveBeenCalledTimes(1);
expect(onMessage).not.toHaveBeenCalled();
});
diff --git a/src/engine/adapters/baileys.adapter.ts b/src/engine/adapters/baileys.adapter.ts
index 306ba08d2..790997c71 100644
--- a/src/engine/adapters/baileys.adapter.ts
+++ b/src/engine/adapters/baileys.adapter.ts
@@ -93,11 +93,6 @@ export class BaileysAdapter implements IWhatsAppEngine {
private set sock(value: WASocket | null) {
this.lifecycle.sock = value;
}
- /** Unix-seconds timestamp of the last 'open' connection.update — the events delegate's
- * live-vs-history discriminator, read live; the value is owned by the lifecycle delegate. */
- private get connectedAt(): number {
- return this.lifecycle.connectedAt;
- }
/** Live-call cache handle — the map is owned by the events delegate (call events + rejectCall);
* lifecycle teardown clears it so a late rejectCall() reports not-found on a dead socket. The
* adapter keeps this alias for the unmodified spec, which reads `adapter.liveCalls` via a cast. */
@@ -120,13 +115,7 @@ export class BaileysAdapter implements IWhatsAppEngine {
// is added once here, not to nine per-delegate bags. Each delegate keeps its own narrow Host
// interface, which this literal satisfies structurally - least privilege stays enforceable.
const delegates: { events?: BaileysEvents } = {};
- const connectedAt = (): number => this.connectedAt;
const host: BaileysEngineHost = {
- // An object-literal getter's `this` is the literal itself, so the live connectedAt read goes
- // through the arrow closure above, which captures the adapter.
- get connectedAt() {
- return connectedAt();
- },
getSocket: () => this.sock!,
getSocketOrNull: () => this.sock,
logger: this.logger,