Skip to content

Fix messages appearing out of order when local time is not synced with server time#6193

Draft
VelikovPetar wants to merge 1 commit intodevelopfrom
bug/AND-1069_fix_messages_out_of_order
Draft

Fix messages appearing out of order when local time is not synced with server time#6193
VelikovPetar wants to merge 1 commit intodevelopfrom
bug/AND-1069_fix_messages_out_of_order

Conversation

@VelikovPetar
Copy link
Contributor

@VelikovPetar VelikovPetar commented Feb 25, 2026

Goal

Fix messages appearing out of order when a user's device clock is not in sync with the server, or when multiple messages are sent in rapid succession and their server-assigned timestamps don't preserve the local send order.

Implementation

Introduced MessageSortComparator — a dedicated Comparator<Message> that uses a two-tier sorting strategy:

  • Same user + both messages have createdLocallyAt: sort by createdLocallyAt to preserve the exact order the messages were composed, regardless of server-side timestamp assignment or network latency.
  • All other pairs: sort by createdAt, falling back to createdLocallyAt when the server timestamp is not yet available. This handles cross-user messages and ensures clock-skewed messages land in the correct position on the timeline.

Replaced all existing .sortedBy { it.getCreatedAtOrNull() } call-sites in ChannelMutableState, ThreadMutableState, Channel.kt (extensions), and Thread.kt (extensions) with .sortedWith(MessageSortComparator).

Why sorting by createdLocallyAt is reliable for same-user messages:

ChatClient.sendMessage calls Message.ensureCreatedLocallyAt() before dispatching each message. That function sets createdLocallyAt to max(channel.lastMessageAt + 1ms, now()), which guarantees the timestamp is always strictly after the previous message in the channel. As a result, successive messages from the same user always have monotonically increasing createdLocallyAt values that reflect the exact order they were composed — making it safe to use as the sort key for same-user pairs.

UI Changes

Before After

Testing

…h server time

Co-Authored-By: Claude <noreply@anthropic.com>
@VelikovPetar VelikovPetar added the pr:bug Bug fix label Feb 25, 2026
@github-actions
Copy link
Contributor

github-actions bot commented Feb 25, 2026

PR checklist ❌

The following issues were detected:

  • Section Testing is missing or empty.

What we check

  1. Title is concise (5–18 words) unless labeled pr:ignore-for-release.
  2. At least one pr: label exists (e.g., pr:bug, pr:new-feature).
  3. Sections ### Goal, ### Implementation, and ### Testing contain content.

@github-actions
Copy link
Contributor

SDK Size Comparison 📏

SDK Before After Difference Status
stream-chat-android-client 5.26 MB 5.26 MB 0.00 MB 🟢
stream-chat-android-offline 5.48 MB 5.48 MB 0.00 MB 🟢
stream-chat-android-ui-components 10.63 MB 10.63 MB 0.00 MB 🟢
stream-chat-android-compose 12.85 MB 12.85 MB 0.00 MB 🟢

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

pr:bug Bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant