Skip to content

Fixed chat loading issues using pagination and resolved session disco… - #1307

Open
gujaratiprakash wants to merge 1 commit into
rmyndharis:mainfrom
gujaratiprakash:chat_session_fixes
Open

Fixed chat loading issues using pagination and resolved session disco…#1307
gujaratiprakash wants to merge 1 commit into
rmyndharis:mainfrom
gujaratiprakash:chat_session_fixes

Conversation

@gujaratiprakash

Copy link
Copy Markdown

Fixed chat loading issues using pagination and resolved session disconnection through persistent session management.

Here is the complete Pull Request description ready to be copied into GitHub:


Fixed chat loading issues using pagination and resolved session disconnection through persistent session management.

Description

This PR addresses performance bottlenecks when loading large chat histories, prevents false transport disconnection errors during CDP command execution, catches unhandled media download rejections, and ensures full WhatsApp session persistence across Docker container rebuilds.

Summary of Major & Minor Changes:

1. Chat Pagination & Scroll UX (Dashboard Frontend & API)

  • API Pagination Support: Updated getChats and getChatMessages in dashboard/src/services/api.ts to accept limit and offset query parameters.
  • Top-Scroll Infinite Loading: Enhanced useChatMessages.ts with fetchPreviousMessages(), hasMore, and isLoadingMore states. In ChatThread.tsx, scrolling near the top (scrollTop < 100px) automatically fetches older message history pages and displays a loading indicator.
  • Scroll Position Lock: Added onOlderMessagesPrepended in useChatScrollPosition.ts to calculate height delta (scrollTop + (newScrollHeight - oldScrollHeight)), keeping the viewport locked on the current message without jumpiness when older messages load.
  • Bottom-to-Top Conversation Flow: Ensured chats open scrolled to the most recent messages at the bottom, maintaining pin alignment on media loads.
  • Sidebar Chat List Caching: Cached sidebar chat queries per session via React Query in Chats.tsx, making tab switching (Chats / Channels / Status) instant with zero network delay.

2. Session Disconnection & CDP Timeout Fixes (Backend & Engine)

  • Unlimited CDP Timeout: Exposed PUPPETEER_PROTOCOL_TIMEOUT (defaulting to 0 / unlimited) in src/config/configuration.ts, WhatsAppWebJsAdapter, WhatsAppWebJsPlugin, and EngineFactory to prevent ProtocolError: Runtime.callFunctionOn timed out during heavy Client.getChats() operations.
  • Refined Transport Classifier: Updated isPageTransportError in whatsapp-web-js.adapter.ts to return false on timed out errors so protocol timeouts no longer trigger false page death reports or disconnect live sessions.
  • Safe Inbound Media Downloads: Added .catch() handling on msg.downloadMedia() in capInboundMediaFor to catch WhatsApp Web Error: Getter was called with undefined data and fall back gracefully to declaredOnlyMedia(msg) without unhandled rejections.

3. Docker Session Persistence & Auto-Reconnection

  • Docker Volume Declaration: Added VOLUME ["/app/data"] and ./data/baileys directory initialization to Dockerfile so WhatsApp Web profiles (./data/sessions), Baileys auth state (./data/baileys), SQLite databases (main.sqlite, openwa.sqlite), API keys, and media survive container rebuilds (docker compose up --build) and recreations.
  • Container Entrypoint Hygiene: Updated docker-entrypoint.sh to initialize permissions for /app/data/baileys alongside sessions and auto-clear stale Chromium locks (rm -f /app/data/sessions/*/Singleton*) at boot.
  • Auto-Start On Container Rebuild: Set AUTO_START_SESSIONS=${AUTO_START_SESSIONS:-true} in docker-compose.yml so authenticated sessions automatically reconnect on container boot.

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update

Checklist

  • Tests added/updated (feature-flags.spec.ts, wwebjs-chat-transport-death.spec.ts, chatMessages.test.ts)
  • Documentation updated (.env.example, inline JSDocs)
  • Lint passes (npm run dashboard:build, tsc -b)
  • Self-reviewed

Screenshots (if applicable)

  • Infinite Scroll: Scrolling up fetches older messages with smooth scroll-position retention and a top loader.
  • Cached Chat List: Instant sidebar tab switching without refetch delays.
  • Bottom-to-Top Chat Load: Recent messages display immediately at the bottom of the viewport on room open.
image

…nnection through persistent session management.

@rmyndharis rmyndharis left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for putting this together — the chat-history pagination in particular addresses a real gap, and we'd like to get a version of it in. This PR mixes several independent concerns, though, and part of it no longer applies to main, so requesting changes with a concrete path forward.

Please rebase first. This branch predates #1308, which moved src/plugins/engines/* to src/engine/builtin/ and split the wwebjs adapter — the launch options and the transport-error classifier now live in wwebjs-lifecycle.ts, so the engine-side hunks won't apply. CI also hasn't run on this PR yet; please make sure the checks pass after the rebase.

Worth keeping, as separate PRs:

  1. Chat history pagination (dashboard) — the most valuable piece. Two implementation notes: the fetch-older offset should count DB rows already fetched rather than the merged array length (the merged list includes engine-history items, so paging by current.length skips DB rows), and useInfiniteQuery from React Query would fit the existing stack nicely. A spec covering the paging behavior would help.
  2. msg.downloadMedia() rejection handling and the optional-chained msg.id — small, correct, happy to take nearly as-is.
  3. CDP timeout handling — the concern is real, but defaulting protocolTimeout to 0 (unlimited) removes the only bound on a hung renderer. We'd prefer a high-but-finite default plus the classifier carve-out, preserving the contract that a dead page is reported as 503 rather than hanging.

Please drop from this PR:

  • VOLUME ["/app/data"] — compose already persists data through the named volume; a bare VOLUME creates anonymous volumes that detach when the container is replaced.
  • Flipping the AUTO_START_SESSIONS default in the production compose — the opt-in is deliberate (see the comment next to that line and .env.example); changing that policy is a separate discussion, and we'd rather not loosen the spec that pins it.
  • nest-cli.json (deleteOutDir: false) — unrelated to the fixes and changes build hygiene for everyone.
  • The package-lock.json churn — dropping the libc fields looks like an older-npm regeneration and can affect musl optional-dependency selection.
  • The Windows path in chromium-profile-hygiene — out of scope here and untestable in the current CI.

One small note: the description mentions updates to chatMessages.test.ts and .env.example, but neither appears in the diff — worth syncing the description with the actual changes.

Happy to review the split PRs — the pagination one especially is something we want.

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.

3 participants