Skip to content

Conversation

@deepak0x
Copy link
Contributor

@deepak0x deepak0x commented Jan 25, 2026

This PR fixes a critical memory leak where the message store could grow without bounds in high-volume channels, eventually leading to application crashes due to out-of-memory errors.

The issue is resolved using a two-part approach:

  • Memory capping to limit how many messages are stored in state.
  • Virtualized rendering to efficiently display messages without DOM or memory overhead.

Closes #1092

Problem

  • The message store kept accumulating messages in active channels.
  • High-traffic rooms could grow the store indefinitely.
  • This caused excessive memory usage and eventual application crashes (OOM).

Solution

  • Enforce a strict upper bound on the number of messages kept in memory.
  • Render messages using virtualization so only visible items exist in the DOM.
  • Delegate scroll and pagination logic to the virtualization layer.

Changes Made

1. Message Store (packages/react/src/store/messageStore.js)

  • Enforced a hard cap of 500 messages in both setMessages and upsertMessage.
  • Added strict chronological sorting by timestamp to maintain correct message order.
  • Logic now always keeps the newest 500 messages and drops the oldest when the limit is exceeded.

2. Virtualized Rendering (packages/react/src/views/MessageList/MessageList.js)

  • Replaced direct .map() rendering with react-virtuoso.
  • Only visible messages are rendered in the DOM, greatly reducing memory and render cost.
  • Configured with alignToBottom to preserve standard chat behavior.

3. Scroll Management (packages/react/src/views/ChatBody/ChatBody.js)

  • Refactored ChatBody to delegate scroll handling to Virtuoso.
  • Removed manual scroll listeners and calculations.
  • Connected loadMoreMessages to Virtuoso’s startReached callback to support seamless history loading up to the capped limit.

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.

Bug: Unbounded Memory Growth in Message Store Causing OOM Crashes

1 participant