Skip to content

feat(baileys): externalize reconnect via internalReconnect=false + recreateSocket() - #5

Merged
kobie3717 merged 3 commits into
mainfrom
feat/external-reconnect
May 4, 2026
Merged

kobie3717 merged 3 commits into
mainfrom
feat/external-reconnect

Conversation

@kobie3717

Copy link
Copy Markdown
Owner

Summary

Lets consumers own the reconnect lifecycle (e.g. via baileys-keep-alive's reconnectFactory) by gating BaileysProvider's internal setTimeout-driven retries behind a new `internalReconnect` option (default `false`).

When `internalReconnect: false`:

  • The provider no longer schedules its own `setTimeout(() => this.connect(...))` for transient close events (badMac, streamError, generic recoverable).
  • Instead it always emits `'disconnected'` with `shouldReconnect: true` so a consumer can decide.

Also adds a public `recreateSocket()` helper for use as a `reconnectFactory` — resets connection flags and re-runs `connect()`.

Why

With three layers of reconnect logic (internal setTimeout, WaSP SESSION_DISCONNECTED, baileys-keep-alive) racing each other, downstream sessions held stale socket references after the provider's internal swap, surfacing as `Boom('Connection Closed')` on every send.

This change establishes a single source of truth for reconnect, owned by the consumer.

Test plan

  • Builds (`npm run build`)
  • Existing test suite passes
  • Live verified end-to-end with whatshub + baileys-keep-alive (send works after restart, heartbeat running)

🤖 Generated with Claude Code

root and others added 3 commits May 2, 2026 13:12
Provider 'disconnected' event left the session entry in activeSessions,
so consumer reconnect via createSession() threw "Session already exists"
and the connection stayed dropped until process restart.

Now remove the entry (and detach listeners) before re-emitting
SESSION_DISCONNECTED, so callers can recreate cleanly on transient drops
(Baileys reason -1, 515, 503).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Default-off internal setTimeout-driven reconnect so consumers (e.g.
baileys-keep-alive) can own the reconnect lifecycle. When
internalReconnect=false, the provider always emits 'disconnected' on
close (transient and terminal alike) so the consumer can react.

Adds public recreateSocket() helper that resets the connection state
and re-runs connect() — designed for use as a keepAlive
reconnectFactory.

Resolves the dead-cache class of bugs where BaileysProvider silently
swapped its internal socket and downstream sessions held a stale
reference.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@kobie3717
kobie3717 merged commit 7d2750b into main May 4, 2026
2 of 4 checks passed
@kobie3717
kobie3717 deleted the feat/external-reconnect branch May 4, 2026 08:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant